blob: 5259d502add6a76405cd1c2401b222c6157b8761 [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);
297static void acpi_video_switch_brightness(struct acpi_video_device *device,
298 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);
Yu Luming2f3d0002006-11-11 02:40:34 +0800311 acpi_video_device_lcd_get_level_current(vd, &cur_level);
Matthew Garrett38531e62007-12-26 02:03:26 +0000312 for (i = 2; i < vd->brightness->count; i++) {
313 if (vd->brightness->levels[i] == cur_level)
314 /* The first two entries are special - see page 575
315 of the ACPI spec 3.0 */
316 return i-2;
317 }
318 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800319}
320
321static int acpi_video_set_brightness(struct backlight_device *bd)
322{
Matthew Garrett38531e62007-12-26 02:03:26 +0000323 int request_level = bd->props.brightness+2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800324 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100325 (struct acpi_video_device *)bl_get_data(bd);
Matthew Garrett38531e62007-12-26 02:03:26 +0000326 acpi_video_device_lcd_set_level(vd,
327 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800328 return 0;
329}
330
Richard Purdie599a52d2007-02-10 23:07:48 +0000331static struct backlight_ops acpi_backlight_ops = {
332 .get_brightness = acpi_video_get_brightness,
333 .update_status = acpi_video_set_brightness,
334};
335
Luming Yu23b0f012007-05-09 21:07:05 +0800336/*video output device sysfs support*/
337static int acpi_video_output_get(struct output_device *od)
338{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400339 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800340 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700341 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800342 acpi_video_device_get_state(vd, &state);
343 return (int)state;
344}
345
346static int acpi_video_output_set(struct output_device *od)
347{
348 unsigned long state = od->request_state;
349 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700350 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800351 return acpi_video_device_set_state(vd, state);
352}
353
354static struct output_properties acpi_output_properties = {
355 .set_state = acpi_video_output_set,
356 .get_status = acpi_video_output_get,
357};
Zhang Rui702ed512008-01-17 15:51:22 +0800358
359
360/* thermal cooling device callbacks */
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000361static int video_get_max_state(struct thermal_cooling_device *cdev, unsigned
362 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800363{
364 struct acpi_device *device = cdev->devdata;
365 struct acpi_video_device *video = acpi_driver_data(device);
366
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000367 *state = video->brightness->count - 3;
368 return 0;
Zhang Rui702ed512008-01-17 15:51:22 +0800369}
370
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000371static int video_get_cur_state(struct thermal_cooling_device *cdev, unsigned
372 long *state)
Zhang Rui702ed512008-01-17 15:51:22 +0800373{
374 struct acpi_device *device = cdev->devdata;
375 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400376 unsigned long long level;
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000377 int offset;
Zhang Rui702ed512008-01-17 15:51:22 +0800378
379 acpi_video_device_lcd_get_level_current(video, &level);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000380 for (offset = 2; offset < video->brightness->count; offset++)
381 if (level == video->brightness->levels[offset]) {
382 *state = video->brightness->count - offset - 1;
383 return 0;
384 }
Zhang Rui702ed512008-01-17 15:51:22 +0800385
386 return -EINVAL;
387}
388
389static int
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000390video_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
Zhang Rui702ed512008-01-17 15:51:22 +0800391{
392 struct acpi_device *device = cdev->devdata;
393 struct acpi_video_device *video = acpi_driver_data(device);
394 int level;
395
396 if ( state >= video->brightness->count - 2)
397 return -EINVAL;
398
399 state = video->brightness->count - state;
400 level = video->brightness->levels[state -1];
401 return acpi_video_device_lcd_set_level(video, level);
402}
403
404static struct thermal_cooling_device_ops video_cooling_ops = {
405 .get_max_state = video_get_max_state,
406 .get_cur_state = video_get_cur_state,
407 .set_cur_state = video_set_cur_state,
408};
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/* --------------------------------------------------------------------------
411 Video Management
412 -------------------------------------------------------------------------- */
413
414/* device */
415
416static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400417acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Len Brown4be44fc2005-08-05 00:44:28 -0400419 int status;
Patrick Mochel90130262006-05-19 16:54:48 -0400420
421 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Patrick Mocheld550d982006-06-27 00:41:40 -0400423 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426static int
Len Brown4be44fc2005-08-05 00:44:28 -0400427acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400428 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Len Brown4be44fc2005-08-05 00:44:28 -0400430 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Patrick Mochel90130262006-05-19 16:54:48 -0400432 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Patrick Mocheld550d982006-06-27 00:41:40 -0400434 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437static int
Len Brown4be44fc2005-08-05 00:44:28 -0400438acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Len Brown4be44fc2005-08-05 00:44:28 -0400440 int status;
441 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
442 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400443 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400447 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Patrick Mocheld550d982006-06-27 00:41:40 -0400449 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452static int
Len Brown4be44fc2005-08-05 00:44:28 -0400453acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
454 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Len Brown4be44fc2005-08-05 00:44:28 -0400456 int status;
457 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
458 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 *levels = NULL;
462
Patrick Mochel90130262006-05-19 16:54:48 -0400463 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400465 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400466 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500467 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400468 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 status = -EFAULT;
470 goto err;
471 }
472
473 *levels = obj;
474
Patrick Mocheld550d982006-06-27 00:41:40 -0400475 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Len Brown4be44fc2005-08-05 00:44:28 -0400477 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800478 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Patrick Mocheld550d982006-06-27 00:41:40 -0400480 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
483static int
Len Brown4be44fc2005-08-05 00:44:28 -0400484acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400486 int status = AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400487 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
488 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800489 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400494 if (device->cap._BCM)
495 status = acpi_evaluate_object(device->dev->handle, "_BCM",
496 &args, NULL);
497 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800498 for (state = 2; state < device->brightness->count; state++)
499 if (level == device->brightness->levels[state])
500 device->backlight->props.brightness = state - 2;
501
Patrick Mocheld550d982006-06-27 00:41:40 -0400502 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505static int
Len Brown4be44fc2005-08-05 00:44:28 -0400506acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400507 unsigned long long *level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400509 if (device->cap._BQC)
510 return acpi_evaluate_integer(device->dev->handle, "_BQC", NULL,
511 level);
512 *level = device->brightness->curr;
513 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516static int
Len Brown4be44fc2005-08-05 00:44:28 -0400517acpi_video_device_EDID(struct acpi_video_device *device,
518 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Len Brown4be44fc2005-08-05 00:44:28 -0400520 int status;
521 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
522 union acpi_object *obj;
523 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
524 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 *edid = NULL;
528
529 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400530 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (length == 128)
532 arg0.integer.value = 1;
533 else if (length == 256)
534 arg0.integer.value = 2;
535 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400536 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Patrick Mochel90130262006-05-19 16:54:48 -0400538 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400540 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200542 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 if (obj && obj->type == ACPI_TYPE_BUFFER)
545 *edid = obj;
546 else {
Len Brown64684632006-06-26 23:41:38 -0400547 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 status = -EFAULT;
549 kfree(obj);
550 }
551
Patrick Mocheld550d982006-06-27 00:41:40 -0400552 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555/* bus */
556
557static int
Len Brown4be44fc2005-08-05 00:44:28 -0400558acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Len Brown4be44fc2005-08-05 00:44:28 -0400560 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400561 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400562 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
563 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 arg0.integer.value = option;
567
Patrick Mochel90130262006-05-19 16:54:48 -0400568 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400570 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Patrick Mocheld550d982006-06-27 00:41:40 -0400572 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
575static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400576acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 int status;
579
Patrick Mochel90130262006-05-19 16:54:48 -0400580 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Patrick Mocheld550d982006-06-27 00:41:40 -0400582 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585static int
Len Brown4be44fc2005-08-05 00:44:28 -0400586acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400587 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Len Brown4be44fc2005-08-05 00:44:28 -0400589 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Patrick Mochel90130262006-05-19 16:54:48 -0400591 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 *options &= 3;
593
Patrick Mocheld550d982006-06-27 00:41:40 -0400594 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597/*
598 * Arg:
599 * video : video bus device pointer
600 * bios_flag :
601 * 0. The system BIOS should NOT automatically switch(toggle)
602 * the active display output.
603 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100604 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * 2. The _DGS value should be locked.
606 * 3. The system BIOS should not automatically switch (toggle) the
607 * active display output, but instead generate the display switch
608 * event notify code.
609 * lcd_flag :
610 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100611 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100613 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 * Return Value:
615 * -1 wrong arg.
616 */
617
618static int
Len Brown4be44fc2005-08-05 00:44:28 -0400619acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Len Brown4be44fc2005-08-05 00:44:28 -0400621 acpi_integer status = 0;
622 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
623 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Len Brown4be44fc2005-08-05 00:44:28 -0400626 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 status = -1;
628 goto Failed;
629 }
630 arg0.integer.value = (lcd_flag << 2) | bios_flag;
631 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400632 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Len Brown4be44fc2005-08-05 00:44:28 -0400634 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400635 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500639 * Simple comparison function used to sort backlight levels.
640 */
641
642static int
643acpi_video_cmp_level(const void *a, const void *b)
644{
645 return *(int *)a - *(int *)b;
646}
647
648/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 * Arg:
650 * device : video output device (LCD, CRT, ..)
651 *
652 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100653 * Maximum brightness level
654 *
655 * Allocate and initialize device->brightness.
656 */
657
658static int
659acpi_video_init_brightness(struct acpi_video_device *device)
660{
661 union acpi_object *obj = NULL;
662 int i, max_level = 0, count = 0;
663 union acpi_object *o;
664 struct acpi_video_device_brightness *br = NULL;
665
666 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
667 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
668 "LCD brightness level\n"));
669 goto out;
670 }
671
672 if (obj->package.count < 2)
673 goto out;
674
675 br = kzalloc(sizeof(*br), GFP_KERNEL);
676 if (!br) {
677 printk(KERN_ERR "can't allocate memory\n");
678 goto out;
679 }
680
681 br->levels = kmalloc(obj->package.count * sizeof *(br->levels),
682 GFP_KERNEL);
683 if (!br->levels)
684 goto out_free;
685
686 for (i = 0; i < obj->package.count; i++) {
687 o = (union acpi_object *)&obj->package.elements[i];
688 if (o->type != ACPI_TYPE_INTEGER) {
689 printk(KERN_ERR PREFIX "Invalid data\n");
690 continue;
691 }
692 br->levels[count] = (u32) o->integer.value;
693
694 if (br->levels[count] > max_level)
695 max_level = br->levels[count];
696 count++;
697 }
698
Zhang Rui935e5f22008-12-11 16:24:52 -0500699 /* don't sort the first two brightness levels */
700 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
701 acpi_video_cmp_level, NULL);
702
Julia Jomantaite469778c2008-06-23 22:50:42 +0100703 if (count < 2)
704 goto out_free_levels;
705
706 br->count = count;
707 device->brightness = br;
708 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "found %d brightness levels\n", count));
709 kfree(obj);
710 return max_level;
711
712out_free_levels:
713 kfree(br->levels);
714out_free:
715 kfree(br);
716out:
717 device->brightness = NULL;
718 kfree(obj);
719 return 0;
720}
721
722/*
723 * Arg:
724 * device : video output device (LCD, CRT, ..)
725 *
726 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * None
728 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100729 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 * device.
731 */
732
Len Brown4be44fc2005-08-05 00:44:28 -0400733static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 acpi_handle h_dummy1;
Yu Luming2f3d0002006-11-11 02:40:34 +0800736 u32 max_level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
William Lee Irwin III98934de2007-12-12 03:56:55 -0800739 memset(&device->cap, 0, sizeof(device->cap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Patrick Mochel90130262006-05-19 16:54:48 -0400741 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 device->cap._ADR = 1;
743 }
Patrick Mochel90130262006-05-19 16:54:48 -0400744 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400745 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Patrick Mochel90130262006-05-19 16:54:48 -0400747 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400748 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800750 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
751 device->cap._BQC = 1;
Patrick Mochel90130262006-05-19 16:54:48 -0400752 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400753 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Patrick Mochel90130262006-05-19 16:54:48 -0400755 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 device->cap._DCS = 1;
757 }
Patrick Mochel90130262006-05-19 16:54:48 -0400758 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 device->cap._DGS = 1;
760 }
Patrick Mochel90130262006-05-19 16:54:48 -0400761 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 device->cap._DSS = 1;
763 }
764
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200765 if (acpi_video_backlight_support())
766 max_level = acpi_video_init_brightness(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Zhao Yakuic2c78902008-07-17 10:46:05 +0800768 if (device->cap._BCL && device->cap._BCM && max_level > 0) {
Zhang Rui702ed512008-01-17 15:51:22 +0800769 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800770 static int count = 0;
771 char *name;
Yu Luming2f3d0002006-11-11 02:40:34 +0800772 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
773 if (!name)
774 return;
775
Yu Luming2f3d0002006-11-11 02:40:34 +0800776 sprintf(name, "acpi_video%d", count++);
Yu Luming2f3d0002006-11-11 02:40:34 +0800777 device->backlight = backlight_device_register(name,
Richard Purdie599a52d2007-02-10 23:07:48 +0000778 NULL, device, &acpi_backlight_ops);
Matthew Garrett38531e62007-12-26 02:03:26 +0000779 device->backlight->props.max_brightness = device->brightness->count-3;
Zhao Yakuic2c78902008-07-17 10:46:05 +0800780 /*
781 * If there exists the _BQC object, the _BQC object will be
782 * called to get the current backlight brightness. Otherwise
783 * the brightness will be set to the maximum.
784 */
785 if (device->cap._BQC)
786 device->backlight->props.brightness =
787 acpi_video_get_brightness(device->backlight);
788 else
789 device->backlight->props.brightness =
790 device->backlight->props.max_brightness;
Richard Purdie599a52d2007-02-10 23:07:48 +0000791 backlight_update_status(device->backlight);
Yu Luming2f3d0002006-11-11 02:40:34 +0800792 kfree(name);
Zhang Rui702ed512008-01-17 15:51:22 +0800793
794 device->cdev = thermal_cooling_device_register("LCD",
795 device->dev, &video_cooling_ops);
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500796 if (IS_ERR(device->cdev))
797 return;
798
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200799 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
800 device->cdev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800801 result = sysfs_create_link(&device->dev->dev.kobj,
802 &device->cdev->device.kobj,
803 "thermal_cooling");
804 if (result)
805 printk(KERN_ERR PREFIX "Create sysfs link\n");
806 result = sysfs_create_link(&device->cdev->device.kobj,
807 &device->dev->dev.kobj, "device");
808 if (result)
809 printk(KERN_ERR PREFIX "Create sysfs link\n");
810
Yu Luming2f3d0002006-11-11 02:40:34 +0800811 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200812
813 if (acpi_video_display_switch_support()) {
814
815 if (device->cap._DCS && device->cap._DSS) {
816 static int count;
817 char *name;
818 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
819 if (!name)
820 return;
821 sprintf(name, "acpi_video%d", count++);
822 device->output_dev = video_output_register(name,
823 NULL, device, &acpi_output_properties);
824 kfree(name);
825 }
Luming Yu23b0f012007-05-09 21:07:05 +0800826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
829/*
830 * Arg:
831 * device : video output device (VGA)
832 *
833 * Return Value:
834 * None
835 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100836 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 */
838
Len Brown4be44fc2005-08-05 00:44:28 -0400839static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Len Brown4be44fc2005-08-05 00:44:28 -0400841 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
William Lee Irwin III98934de2007-12-12 03:56:55 -0800843 memset(&video->cap, 0, sizeof(video->cap));
Patrick Mochel90130262006-05-19 16:54:48 -0400844 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 video->cap._DOS = 1;
846 }
Patrick Mochel90130262006-05-19 16:54:48 -0400847 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 video->cap._DOD = 1;
849 }
Patrick Mochel90130262006-05-19 16:54:48 -0400850 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 video->cap._ROM = 1;
852 }
Patrick Mochel90130262006-05-19 16:54:48 -0400853 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 video->cap._GPD = 1;
855 }
Patrick Mochel90130262006-05-19 16:54:48 -0400856 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 video->cap._SPD = 1;
858 }
Patrick Mochel90130262006-05-19 16:54:48 -0400859 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 video->cap._VPO = 1;
861 }
862}
863
864/*
865 * Check whether the video bus device has required AML method to
866 * support the desired features
867 */
868
Len Brown4be44fc2005-08-05 00:44:28 -0400869static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Len Brown4be44fc2005-08-05 00:44:28 -0400871 acpi_status status = -ENOENT;
Thomas Renninger22c13f92008-08-01 17:37:54 +0200872 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400875 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Thomas Renninger22c13f92008-08-01 17:37:54 +0200877 dev = acpi_get_physical_pci_device(video->device->handle);
878 if (!dev)
879 return -ENODEV;
880 put_device(dev);
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 /* Since there is no HID, CID and so on for VGA driver, we have
883 * to check well known required nodes.
884 */
885
Julius Volz98fb8fe2007-02-20 16:38:40 +0100886 /* Does this device support video switching? */
Len Brown4be44fc2005-08-05 00:44:28 -0400887 if (video->cap._DOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 video->flags.multihead = 1;
889 status = 0;
890 }
891
Julius Volz98fb8fe2007-02-20 16:38:40 +0100892 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -0400893 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 video->flags.rom = 1;
895 status = 0;
896 }
897
Julius Volz98fb8fe2007-02-20 16:38:40 +0100898 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -0400899 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 video->flags.post = 1;
901 status = 0;
902 }
903
Patrick Mocheld550d982006-06-27 00:41:40 -0400904 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
907/* --------------------------------------------------------------------------
908 FS Interface (/proc)
909 -------------------------------------------------------------------------- */
910
Len Brown4be44fc2005-08-05 00:44:28 -0400911static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913/* video devices */
914
Len Brown4be44fc2005-08-05 00:44:28 -0400915static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200917 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (!dev)
921 goto end;
922
923 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
924 seq_printf(seq, "type: ");
925 if (dev->flags.crt)
926 seq_printf(seq, "CRT\n");
927 else if (dev->flags.lcd)
928 seq_printf(seq, "LCD\n");
929 else if (dev->flags.tvout)
930 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -0500931 else if (dev->flags.dvi)
932 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 else
934 seq_printf(seq, "UNKNOWN\n");
935
Len Brown4be44fc2005-08-05 00:44:28 -0400936 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Len Brown4be44fc2005-08-05 00:44:28 -0400938 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400939 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
942static int
Len Brown4be44fc2005-08-05 00:44:28 -0400943acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 return single_open(file, acpi_video_device_info_seq_show,
946 PDE(inode)->data);
947}
948
Len Brown4be44fc2005-08-05 00:44:28 -0400949static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Len Brown4be44fc2005-08-05 00:44:28 -0400951 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200952 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400953 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 if (!dev)
957 goto end;
958
959 status = acpi_video_device_get_state(dev, &state);
960 seq_printf(seq, "state: ");
961 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -0400962 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 else
964 seq_printf(seq, "<not supported>\n");
965
966 status = acpi_video_device_query(dev, &state);
967 seq_printf(seq, "query: ");
968 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -0400969 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 else
971 seq_printf(seq, "<not supported>\n");
972
Len Brown4be44fc2005-08-05 00:44:28 -0400973 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977static int
Len Brown4be44fc2005-08-05 00:44:28 -0400978acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 return single_open(file, acpi_video_device_state_seq_show,
981 PDE(inode)->data);
982}
983
984static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400985acpi_video_device_write_state(struct file *file,
986 const char __user * buffer,
987 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Len Brown4be44fc2005-08-05 00:44:28 -0400989 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200990 struct seq_file *m = file->private_data;
991 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400992 char str[12] = { 0 };
993 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 if (!dev || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -0400997 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001000 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002 str[count] = 0;
1003 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001004 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 status = acpi_video_device_set_state(dev, state);
1007
1008 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001009 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Patrick Mocheld550d982006-06-27 00:41:40 -04001011 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014static int
Len Brown4be44fc2005-08-05 00:44:28 -04001015acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001017 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001018 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 if (!dev || !dev->brightness) {
1022 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001023 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025
1026 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001027 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 seq_printf(seq, " %d", dev->brightness->levels[i]);
1029 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1030
Patrick Mocheld550d982006-06-27 00:41:40 -04001031 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
1034static int
Len Brown4be44fc2005-08-05 00:44:28 -04001035acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 return single_open(file, acpi_video_device_brightness_seq_show,
1038 PDE(inode)->data);
1039}
1040
1041static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001042acpi_video_device_write_brightness(struct file *file,
1043 const char __user * buffer,
1044 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001046 struct seq_file *m = file->private_data;
1047 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001048 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001049 unsigned int level = 0;
1050 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Thomas Renninger59d399d2005-11-08 05:27:00 -05001053 if (!dev || !dev->brightness || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001054 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001057 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 str[count] = 0;
1060 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001063 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Julius Volz98fb8fe2007-02-20 16:38:40 +01001065 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001066 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (level == dev->brightness->levels[i]) {
Len Brown4be44fc2005-08-05 00:44:28 -04001068 if (ACPI_SUCCESS
1069 (acpi_video_device_lcd_set_level(dev, level)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 dev->brightness->curr = level;
1071 break;
1072 }
1073
Patrick Mocheld550d982006-06-27 00:41:40 -04001074 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
Len Brown4be44fc2005-08-05 00:44:28 -04001077static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001079 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001080 int status;
1081 int i;
1082 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if (!dev)
1086 goto out;
1087
Len Brown4be44fc2005-08-05 00:44:28 -04001088 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001090 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092
1093 if (ACPI_FAILURE(status)) {
1094 goto out;
1095 }
1096
1097 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1098 for (i = 0; i < edid->buffer.length; i++)
1099 seq_putc(seq, edid->buffer.pointer[i]);
1100 }
1101
Len Brown4be44fc2005-08-05 00:44:28 -04001102 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (!edid)
1104 seq_printf(seq, "<not supported>\n");
1105 else
1106 kfree(edid);
1107
Patrick Mocheld550d982006-06-27 00:41:40 -04001108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
1111static int
Len Brown4be44fc2005-08-05 00:44:28 -04001112acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 return single_open(file, acpi_video_device_EDID_seq_show,
1115 PDE(inode)->data);
1116}
1117
Len Brown4be44fc2005-08-05 00:44:28 -04001118static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001120 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 struct acpi_video_device *vid_dev;
1122
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001123 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001125 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001127 device_dir = proc_mkdir(acpi_device_bid(device),
1128 vid_dev->video->dir);
1129 if (!device_dir)
1130 return -ENOMEM;
1131
1132 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001135 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001136 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001138 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001141 acpi_video_device_state_fops.write = acpi_video_device_write_state;
1142 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001143 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001144 &acpi_video_device_state_fops,
1145 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001147 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001150 acpi_video_device_brightness_fops.write =
1151 acpi_video_device_write_brightness;
1152 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001153 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001154 &acpi_video_device_brightness_fops,
1155 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001157 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001160 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001161 &acpi_video_device_EDID_fops,
1162 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001164 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001166 acpi_device_dir(device) = device_dir;
1167
Patrick Mocheld550d982006-06-27 00:41:40 -04001168 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001169
1170 err_remove_brightness:
1171 remove_proc_entry("brightness", device_dir);
1172 err_remove_state:
1173 remove_proc_entry("state", device_dir);
1174 err_remove_info:
1175 remove_proc_entry("info", device_dir);
1176 err_remove_dir:
1177 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1178 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
Len Brown4be44fc2005-08-05 00:44:28 -04001181static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
1183 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001184 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001186 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001188 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001190 device_dir = acpi_device_dir(device);
1191 if (device_dir) {
1192 remove_proc_entry("info", device_dir);
1193 remove_proc_entry("state", device_dir);
1194 remove_proc_entry("brightness", device_dir);
1195 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001196 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 acpi_device_dir(device) = NULL;
1198 }
1199
Patrick Mocheld550d982006-06-27 00:41:40 -04001200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001204static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001206 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 if (!video)
1210 goto end;
1211
1212 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001213 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001215 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001217 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Len Brown4be44fc2005-08-05 00:44:28 -04001219 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Len Brown4be44fc2005-08-05 00:44:28 -04001223static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Len Brown4be44fc2005-08-05 00:44:28 -04001225 return single_open(file, acpi_video_bus_info_seq_show,
1226 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Len Brown4be44fc2005-08-05 00:44:28 -04001229static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001231 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 if (!video)
1235 goto end;
1236
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001237 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 seq_printf(seq, "<TODO>\n");
1239
Len Brown4be44fc2005-08-05 00:44:28 -04001240 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Len Brown4be44fc2005-08-05 00:44:28 -04001244static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1247}
1248
Len Brown4be44fc2005-08-05 00:44:28 -04001249static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001251 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001252 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001253 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 if (!video)
1257 goto end;
1258
1259 status = acpi_video_bus_POST_options(video, &options);
1260 if (ACPI_SUCCESS(status)) {
1261 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001262 printk(KERN_WARNING PREFIX
1263 "The motherboard VGA device is not listed as a possible POST device.\n");
1264 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001265 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
Frank Seidel4d939152009-02-04 17:03:07 +01001267 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001268 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (options & 2)
1270 seq_printf(seq, " <PCI video>");
1271 if (options & 4)
1272 seq_printf(seq, " <AGP video>");
1273 seq_putc(seq, '\n');
1274 } else
1275 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001276 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
1280static int
Len Brown4be44fc2005-08-05 00:44:28 -04001281acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Len Brown4be44fc2005-08-05 00:44:28 -04001283 return single_open(file, acpi_video_bus_POST_info_seq_show,
1284 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Len Brown4be44fc2005-08-05 00:44:28 -04001287static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001289 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001290 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001291 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 if (!video)
1295 goto end;
1296
Len Brown4be44fc2005-08-05 00:44:28 -04001297 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (!ACPI_SUCCESS(status)) {
1299 seq_printf(seq, "<not supported>\n");
1300 goto end;
1301 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001302 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Len Brown4be44fc2005-08-05 00:44:28 -04001304 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
Len Brown4be44fc2005-08-05 00:44:28 -04001308static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001310 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Len Brown4be44fc2005-08-05 00:44:28 -04001313 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Patrick Mocheld550d982006-06-27 00:41:40 -04001315 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Len Brown4be44fc2005-08-05 00:44:28 -04001318static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Len Brown4be44fc2005-08-05 00:44:28 -04001320 return single_open(file, acpi_video_bus_POST_seq_show,
1321 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
Len Brown4be44fc2005-08-05 00:44:28 -04001324static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
1326 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1327}
1328
1329static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001330acpi_video_bus_write_POST(struct file *file,
1331 const char __user * buffer,
1332 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Len Brown4be44fc2005-08-05 00:44:28 -04001334 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001335 struct seq_file *m = file->private_data;
1336 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001337 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001338 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001342 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 status = acpi_video_bus_POST_options(video, &options);
1345 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001346 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001349 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 str[count] = 0;
1352 opt = strtoul(str, NULL, 0);
1353 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001354 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Julius Volz98fb8fe2007-02-20 16:38:40 +01001356 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 options |= 1;
1358
1359 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001360 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001362 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 }
1365
Patrick Mocheld550d982006-06-27 00:41:40 -04001366 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
1369static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001370acpi_video_bus_write_DOS(struct file *file,
1371 const char __user * buffer,
1372 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Len Brown4be44fc2005-08-05 00:44:28 -04001374 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001375 struct seq_file *m = file->private_data;
1376 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001377 char str[12] = { 0 };
1378 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001382 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001385 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 str[count] = 0;
1388 opt = strtoul(str, NULL, 0);
1389 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001390 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Len Brown4be44fc2005-08-05 00:44:28 -04001392 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001395 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Patrick Mocheld550d982006-06-27 00:41:40 -04001397 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Len Brown4be44fc2005-08-05 00:44:28 -04001400static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001402 struct acpi_video_bus *video = acpi_driver_data(device);
1403 struct proc_dir_entry *device_dir;
1404 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001406 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1407 if (!device_dir)
1408 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001410 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001413 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001414 &acpi_video_bus_info_fops,
1415 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001417 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001420 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001421 &acpi_video_bus_ROM_fops,
1422 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001424 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001427 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001428 &acpi_video_bus_POST_info_fops,
1429 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001431 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 /* 'POST' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001434 acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001435 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001436 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001437 &acpi_video_bus_POST_fops,
1438 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001440 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 /* 'DOS' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001443 acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001444 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001445 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001446 &acpi_video_bus_DOS_fops,
1447 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001449 goto err_remove_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001451 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001452 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001453
1454 err_remove_post:
1455 remove_proc_entry("POST", device_dir);
1456 err_remove_post_info:
1457 remove_proc_entry("POST_info", device_dir);
1458 err_remove_rom:
1459 remove_proc_entry("ROM", device_dir);
1460 err_remove_info:
1461 remove_proc_entry("info", device_dir);
1462 err_remove_dir:
1463 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1464 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
Len Brown4be44fc2005-08-05 00:44:28 -04001467static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001469 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001471 if (device_dir) {
1472 remove_proc_entry("info", device_dir);
1473 remove_proc_entry("ROM", device_dir);
1474 remove_proc_entry("POST_info", device_dir);
1475 remove_proc_entry("POST", device_dir);
1476 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001477 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 acpi_device_dir(device) = NULL;
1479 }
1480
Patrick Mocheld550d982006-06-27 00:41:40 -04001481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
1484/* --------------------------------------------------------------------------
1485 Driver Interface
1486 -------------------------------------------------------------------------- */
1487
1488/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001489static struct acpi_video_device_attrib*
1490acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1491{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001492 struct acpi_video_enumerated_device *ids;
1493 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001494
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001495 for (i = 0; i < video->attached_count; i++) {
1496 ids = &video->attached_array[i];
1497 if ((ids->value.int_val & 0xffff) == device_id)
1498 return &ids->value.attrib;
1499 }
1500
Rui Zhang82cae992007-01-03 23:40:53 -05001501 return NULL;
1502}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504static int
Len Brown4be44fc2005-08-05 00:44:28 -04001505acpi_video_bus_get_one_device(struct acpi_device *device,
1506 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001508 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001509 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001510 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001511 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001514 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Len Brown4be44fc2005-08-05 00:44:28 -04001516 status =
1517 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (ACPI_SUCCESS(status)) {
1519
Burman Yan36bcbec2006-12-19 12:56:11 -08001520 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001522 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1525 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001526 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 data->device_id = device_id;
1529 data->video = video;
1530 data->dev = device;
1531
Rui Zhang82cae992007-01-03 23:40:53 -05001532 attribute = acpi_video_get_device_attr(video, device_id);
1533
1534 if((attribute != NULL) && attribute->device_id_scheme) {
1535 switch (attribute->display_type) {
1536 case ACPI_VIDEO_DISPLAY_CRT:
1537 data->flags.crt = 1;
1538 break;
1539 case ACPI_VIDEO_DISPLAY_TV:
1540 data->flags.tvout = 1;
1541 break;
1542 case ACPI_VIDEO_DISPLAY_DVI:
1543 data->flags.dvi = 1;
1544 break;
1545 case ACPI_VIDEO_DISPLAY_LCD:
1546 data->flags.lcd = 1;
1547 break;
1548 default:
1549 data->flags.unknown = 1;
1550 break;
1551 }
1552 if(attribute->bios_can_detect)
1553 data->flags.bios = 1;
1554 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 acpi_video_device_bind(video, data);
1558 acpi_video_device_find_cap(data);
1559
Patrick Mochel90130262006-05-19 16:54:48 -04001560 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001561 ACPI_DEVICE_NOTIFY,
1562 acpi_video_device_notify,
1563 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001565 printk(KERN_ERR PREFIX
1566 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001567 if(data->brightness)
1568 kfree(data->brightness->levels);
1569 kfree(data->brightness);
1570 kfree(data);
1571 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
1573
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001574 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001576 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 acpi_video_device_add_fs(device);
1579
Patrick Mocheld550d982006-06-27 00:41:40 -04001580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582
Patrick Mocheld550d982006-06-27 00:41:40 -04001583 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
1586/*
1587 * Arg:
1588 * video : video bus device
1589 *
1590 * Return:
1591 * none
1592 *
1593 * Enumerate the video device list of the video bus,
1594 * bind the ids with the corresponding video devices
1595 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001596 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Len Brown4be44fc2005-08-05 00:44:28 -04001598static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001600 struct acpi_video_device *dev;
1601
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001602 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001603
1604 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001605 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001606
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001607 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
1610/*
1611 * Arg:
1612 * video : video bus device
1613 * device : video output device under the video
1614 * bus
1615 *
1616 * Return:
1617 * none
1618 *
1619 * Bind the ids with the corresponding video devices
1620 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001621 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623static void
Len Brown4be44fc2005-08-05 00:44:28 -04001624acpi_video_device_bind(struct acpi_video_bus *video,
1625 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001627 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001628 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001630 for (i = 0; i < video->attached_count; i++) {
1631 ids = &video->attached_array[i];
1632 if (device->device_id == (ids->value.int_val & 0xffff)) {
1633 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1635 }
1636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
1639/*
1640 * Arg:
1641 * video : video bus device
1642 *
1643 * Return:
1644 * < 0 : error
1645 *
1646 * Call _DOD to enumerate all devices attached to display adapter
1647 *
Len Brown4be44fc2005-08-05 00:44:28 -04001648 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1651{
Len Brown4be44fc2005-08-05 00:44:28 -04001652 int status;
1653 int count;
1654 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001655 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001656 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1657 union acpi_object *dod = NULL;
1658 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Patrick Mochel90130262006-05-19 16:54:48 -04001660 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001662 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001663 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001666 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001668 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 status = -EFAULT;
1670 goto out;
1671 }
1672
1673 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001674 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001676 active_list = kcalloc(1 + dod->package.count,
1677 sizeof(struct acpi_video_enumerated_device),
1678 GFP_KERNEL);
1679 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 status = -ENOMEM;
1681 goto out;
1682 }
1683
1684 count = 0;
1685 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001686 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001689 printk(KERN_ERR PREFIX
1690 "Invalid _DOD data in element %d\n", i);
1691 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001693
1694 active_list[count].value.int_val = obj->integer.value;
1695 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001696 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1697 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 count++;
1699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Jesper Juhl6044ec82005-11-07 01:01:32 -08001701 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001702
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001703 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001705
1706 out:
Len Brown02438d82006-06-30 03:19:10 -04001707 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001708 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
Len Brown4be44fc2005-08-05 00:44:28 -04001711static int
1712acpi_video_get_next_level(struct acpi_video_device *device,
1713 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001715 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001716 max = max_below = 0;
1717 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001718 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001719 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001720 l = device->brightness->levels[i];
1721 if (abs(l - level_current) < abs(delta)) {
1722 delta = l - level_current;
1723 if (!delta)
1724 break;
1725 }
1726 }
1727 /* Ajust level_current to closest available level */
1728 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001729 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001730 l = device->brightness->levels[i];
1731 if (l < min)
1732 min = l;
1733 if (l > max)
1734 max = l;
1735 if (l < min_above && l > level_current)
1736 min_above = l;
1737 if (l > max_below && l < level_current)
1738 max_below = l;
1739 }
1740
1741 switch (event) {
1742 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1743 return (level_current < max) ? min_above : min;
1744 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1745 return (level_current < max) ? min_above : max;
1746 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1747 return (level_current > min) ? max_below : min;
1748 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1749 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1750 return 0;
1751 default:
1752 return level_current;
1753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756static void
Len Brown4be44fc2005-08-05 00:44:28 -04001757acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001759 unsigned long long level_current, level_next;
Julia Jomantaite469778c2008-06-23 22:50:42 +01001760 if (!device->brightness)
1761 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 acpi_video_device_lcd_get_level_current(device, &level_current);
1763 level_next = acpi_video_get_next_level(device, level_current, event);
1764 acpi_video_device_lcd_set_level(device, level_next);
1765}
1766
1767static int
Len Brown4be44fc2005-08-05 00:44:28 -04001768acpi_video_bus_get_devices(struct acpi_video_bus *video,
1769 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Len Brown4be44fc2005-08-05 00:44:28 -04001771 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001772 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 acpi_video_device_enumerate(video);
1775
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001776 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 status = acpi_video_bus_get_one_device(dev, video);
1779 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001780 printk(KERN_WARNING PREFIX
1781 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 continue;
1783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001785 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
Len Brown4be44fc2005-08-05 00:44:28 -04001788static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
Karol Kozimor031ec772005-07-30 04:18:00 -04001790 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 struct acpi_video_bus *video;
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001795 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 video = device->video;
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 acpi_video_device_remove_fs(device->dev);
1800
Patrick Mochel90130262006-05-19 16:54:48 -04001801 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001802 ACPI_DEVICE_NOTIFY,
1803 acpi_video_device_notify);
Richard Purdie599a52d2007-02-10 23:07:48 +00001804 backlight_device_unregister(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +08001805 if (device->cdev) {
1806 sysfs_remove_link(&device->dev->dev.kobj,
1807 "thermal_cooling");
1808 sysfs_remove_link(&device->cdev->device.kobj,
1809 "device");
1810 thermal_cooling_device_unregister(device->cdev);
1811 device->cdev = NULL;
1812 }
Luming Yu23b0f012007-05-09 21:07:05 +08001813 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001814
Patrick Mocheld550d982006-06-27 00:41:40 -04001815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
Len Brown4be44fc2005-08-05 00:44:28 -04001818static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Len Brown4be44fc2005-08-05 00:44:28 -04001820 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001821 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001823 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001825 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001827 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04001828 if (ACPI_FAILURE(status))
1829 printk(KERN_WARNING PREFIX
1830 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001832 if (dev->brightness) {
1833 kfree(dev->brightness->levels);
1834 kfree(dev->brightness);
1835 }
1836 list_del(&dev->entry);
1837 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001840 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001841
Patrick Mocheld550d982006-06-27 00:41:40 -04001842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
1845/* acpi_video interface */
1846
Len Brown4be44fc2005-08-05 00:44:28 -04001847static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Zhang Ruia21101c2007-09-14 11:46:22 +08001849 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
Len Brown4be44fc2005-08-05 00:44:28 -04001852static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854 return acpi_video_bus_DOS(video, 0, 1);
1855}
1856
Len Brown4be44fc2005-08-05 00:44:28 -04001857static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001859 struct acpi_video_bus *video = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001860 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001861 struct input_dev *input;
1862 int keycode;
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001865 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001867 device = video->device;
Luming Yue9dab192007-08-20 18:23:53 +08001868 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001871 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04001873 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001874 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 break;
1876
Julius Volz98fb8fe2007-02-20 16:38:40 +01001877 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 * connector. */
1879 acpi_video_device_enumerate(video);
1880 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04001881 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001882 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 break;
1884
Len Brown4be44fc2005-08-05 00:44:28 -04001885 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001886 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001887 keycode = KEY_SWITCHVIDEOMODE;
1888 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001889 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001890 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001891 keycode = KEY_VIDEO_NEXT;
1892 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001893 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04001894 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001895 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 break;
1897
1898 default:
Luming Yue9dab192007-08-20 18:23:53 +08001899 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001901 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 break;
1903 }
1904
Zhang Rui7761f632008-01-25 14:48:12 +08001905 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001906 input_report_key(input, keycode, 1);
1907 input_sync(input);
1908 input_report_key(input, keycode, 0);
1909 input_sync(input);
1910
Patrick Mocheld550d982006-06-27 00:41:40 -04001911 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
Len Brown4be44fc2005-08-05 00:44:28 -04001914static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001916 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001917 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001918 struct acpi_video_bus *bus;
1919 struct input_dev *input;
1920 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001923 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001925 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001926 bus = video_device->video;
1927 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001930 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001931 if (brightness_switch_enabled)
1932 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001933 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001934 keycode = KEY_BRIGHTNESS_CYCLE;
1935 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001936 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001937 if (brightness_switch_enabled)
1938 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001939 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001940 keycode = KEY_BRIGHTNESSUP;
1941 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001942 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001943 if (brightness_switch_enabled)
1944 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001945 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001946 keycode = KEY_BRIGHTNESSDOWN;
1947 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001948 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08001949 if (brightness_switch_enabled)
1950 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001951 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001952 keycode = KEY_BRIGHTNESS_ZERO;
1953 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001954 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08001955 if (brightness_switch_enabled)
1956 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04001957 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001958 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 break;
1960 default:
Luming Yue9dab192007-08-20 18:23:53 +08001961 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001963 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 break;
1965 }
Luming Yue9dab192007-08-20 18:23:53 +08001966
Zhang Rui7761f632008-01-25 14:48:12 +08001967 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001968 input_report_key(input, keycode, 1);
1969 input_sync(input);
1970 input_report_key(input, keycode, 0);
1971 input_sync(input);
1972
Patrick Mocheld550d982006-06-27 00:41:40 -04001973 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
Zhang Ruie6d9da12007-08-25 02:23:31 -04001976static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08001977static int acpi_video_resume(struct acpi_device *device)
1978{
1979 struct acpi_video_bus *video;
1980 struct acpi_video_device *video_device;
1981 int i;
1982
1983 if (!device || !acpi_driver_data(device))
1984 return -EINVAL;
1985
1986 video = acpi_driver_data(device);
1987
1988 for (i = 0; i < video->attached_count; i++) {
1989 video_device = video->attached_array[i].bind_info;
1990 if (video_device && video_device->backlight)
1991 acpi_video_set_brightness(video_device->backlight);
1992 }
1993 return AE_OK;
1994}
1995
Len Brown4be44fc2005-08-05 00:44:28 -04001996static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001998 acpi_status status;
1999 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002000 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002001 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Burman Yan36bcbec2006-12-19 12:56:11 -08002003 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002005 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Zhang Ruie6d9da12007-08-25 02:23:31 -04002007 /* a hack to fix the duplicate name "VID" problem on T61 */
2008 if (!strcmp(device->pnp.bus_id, "VID")) {
2009 if (instance)
2010 device->pnp.bus_id[3] = '0' + instance;
2011 instance ++;
2012 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002013 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2014 if (!strcmp(device->pnp.bus_id, "VGA")) {
2015 if (instance)
2016 device->pnp.bus_id[3] = '0' + instance;
2017 instance++;
2018 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002019
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002020 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2022 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002023 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002026 error = acpi_video_bus_check(video);
2027 if (error)
2028 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002030 error = acpi_video_bus_add_fs(device);
2031 if (error)
2032 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002034 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 INIT_LIST_HEAD(&video->video_device_list);
2036
2037 acpi_video_bus_get_devices(video, device);
2038 acpi_video_bus_start_devices(video);
2039
Patrick Mochel90130262006-05-19 16:54:48 -04002040 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002041 ACPI_DEVICE_NOTIFY,
2042 acpi_video_bus_notify, video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002044 printk(KERN_ERR PREFIX
2045 "Error installing notify handler\n");
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002046 error = -ENODEV;
2047 goto err_stop_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
2049
Luming Yue9dab192007-08-20 18:23:53 +08002050 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002051 if (!input) {
2052 error = -ENOMEM;
2053 goto err_uninstall_notify;
2054 }
Luming Yue9dab192007-08-20 18:23:53 +08002055
2056 snprintf(video->phys, sizeof(video->phys),
2057 "%s/video/input0", acpi_device_hid(video->device));
2058
2059 input->name = acpi_device_name(video->device);
2060 input->phys = video->phys;
2061 input->id.bustype = BUS_HOST;
2062 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002063 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002064 input->evbit[0] = BIT(EV_KEY);
2065 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2066 set_bit(KEY_VIDEO_NEXT, input->keybit);
2067 set_bit(KEY_VIDEO_PREV, input->keybit);
2068 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2069 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2070 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2071 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2072 set_bit(KEY_DISPLAY_OFF, input->keybit);
2073 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002074
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002075 error = input_register_device(input);
2076 if (error)
2077 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002080 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2081 video->flags.multihead ? "yes" : "no",
2082 video->flags.rom ? "yes" : "no",
2083 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002087 err_free_input_dev:
2088 input_free_device(input);
2089 err_uninstall_notify:
2090 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
2091 acpi_video_bus_notify);
2092 err_stop_video:
2093 acpi_video_bus_stop_devices(video);
2094 acpi_video_bus_put_devices(video);
2095 kfree(video->attached_array);
2096 acpi_video_bus_remove_fs(device);
2097 err_free_video:
2098 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002099 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002100
2101 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
Len Brown4be44fc2005-08-05 00:44:28 -04002104static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Len Brown4be44fc2005-08-05 00:44:28 -04002106 acpi_status status = 0;
2107 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002111 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002113 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
2115 acpi_video_bus_stop_devices(video);
2116
Patrick Mochel90130262006-05-19 16:54:48 -04002117 status = acpi_remove_notify_handler(video->device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002118 ACPI_DEVICE_NOTIFY,
2119 acpi_video_bus_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 acpi_video_bus_put_devices(video);
2122 acpi_video_bus_remove_fs(device);
2123
Luming Yue9dab192007-08-20 18:23:53 +08002124 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002125 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 kfree(video);
2127
Patrick Mocheld550d982006-06-27 00:41:40 -04002128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
Len Brown4be44fc2005-08-05 00:44:28 -04002131static int __init acpi_video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Len Brown4be44fc2005-08-05 00:44:28 -04002133 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2136 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002137 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 acpi_video_dir->owner = THIS_MODULE;
2139
2140 result = acpi_bus_register_driver(&acpi_video_bus);
2141 if (result < 0) {
2142 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002143 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145
Patrick Mocheld550d982006-06-27 00:41:40 -04002146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Len Brown4be44fc2005-08-05 00:44:28 -04002149static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 acpi_bus_unregister_driver(&acpi_video_bus);
2153
2154 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2155
Patrick Mocheld550d982006-06-27 00:41:40 -04002156 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
2159module_init(acpi_video_init);
2160module_exit(acpi_video_exit);