blob: f0e6eb5341618a3dcef696294df2ec1901fc76b4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * video.c - ACPI Video Driver ($Revision:$)
3 *
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 * Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
Thomas Tuttlef4715182006-12-19 12:56:14 -08006 * Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27#include <linux/kernel.h>
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/types.h>
31#include <linux/list.h>
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -050032#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
Luming Yue9dab192007-08-20 18:23:53 +080035#include <linux/input.h>
Yu Luming2f3d0002006-11-11 02:40:34 +080036#include <linux/backlight.h>
Zhang Rui702ed512008-01-17 15:51:22 +080037#include <linux/thermal.h>
Luming Yu23b0f012007-05-09 21:07:05 +080038#include <linux/video_output.h>
Zhang Rui935e5f22008-12-11 16:24:52 -050039#include <linux/sort.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/uaccess.h>
41
42#include <acpi/acpi_bus.h>
43#include <acpi/acpi_drivers.h>
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define ACPI_VIDEO_CLASS "video"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define ACPI_VIDEO_BUS_NAME "Video Bus"
47#define ACPI_VIDEO_DEVICE_NAME "Video Device"
48#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
49#define ACPI_VIDEO_NOTIFY_PROBE 0x81
50#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
51#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
52#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
53
Thomas Tuttlef4715182006-12-19 12:56:14 -080054#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
55#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
56#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
57#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
58#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Yu Luming2f3d0002006-11-11 02:40:34 +080060#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Rui Zhang82cae992007-01-03 23:40:53 -050062#define ACPI_VIDEO_DISPLAY_CRT 1
63#define ACPI_VIDEO_DISPLAY_TV 2
64#define ACPI_VIDEO_DISPLAY_DVI 3
65#define ACPI_VIDEO_DISPLAY_LCD 4
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050068ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Len Brownf52fd662007-02-12 22:42:12 -050070MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050071MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070072MODULE_LICENSE("GPL");
73
Zhang Rui8a681a42008-01-25 14:47:49 +080074static int brightness_switch_enabled = 1;
75module_param(brightness_switch_enabled, bool, 0644);
76
Len Brown4be44fc2005-08-05 00:44:28 -040077static int acpi_video_bus_add(struct acpi_device *device);
78static int acpi_video_bus_remove(struct acpi_device *device, int type);
Matthew Garrett863c1492008-02-04 23:31:24 -080079static int acpi_video_resume(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Thomas Renninger1ba90e32007-07-23 14:44:41 +020081static const struct acpi_device_id video_device_ids[] = {
82 {ACPI_VIDEO_HID, 0},
83 {"", 0},
84};
85MODULE_DEVICE_TABLE(acpi, video_device_ids);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -050088 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020090 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .ops = {
92 .add = acpi_video_bus_add,
93 .remove = acpi_video_bus_remove,
Matthew Garrett863c1492008-02-04 23:31:24 -080094 .resume = acpi_video_resume,
Len Brown4be44fc2005-08-05 00:44:28 -040095 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
98struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -040099 u8 multihead:1; /* can switch video heads */
100 u8 rom:1; /* can retrieve a video rom */
101 u8 post:1; /* can configure the head to */
102 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105struct acpi_video_bus_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400106 u8 _DOS:1; /*Enable/Disable output switching */
107 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
108 u8 _ROM:1; /*Get ROM Data */
109 u8 _GPD:1; /*Get POST Device */
110 u8 _SPD:1; /*Set POST Device */
111 u8 _VPO:1; /*Video POST Options */
112 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
Len Brown4be44fc2005-08-05 00:44:28 -0400115struct acpi_video_device_attrib {
116 u32 display_index:4; /* A zero-based instance of the Display */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100117 u32 display_port_attachment:4; /*This field differentiates the display type */
Len Brown4be44fc2005-08-05 00:44:28 -0400118 u32 display_type:4; /*Describe the specific type in use */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100119 u32 vendor_specific:4; /*Chipset Vendor Specific */
Len Brown4be44fc2005-08-05 00:44:28 -0400120 u32 bios_can_detect:1; /*BIOS can detect the device */
121 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
122 the VGA device. */
123 u32 pipe_id:3; /*For VGA multiple-head devices. */
124 u32 reserved:10; /*Must be 0 */
125 u32 device_id_scheme:1; /*Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128struct acpi_video_enumerated_device {
129 union {
130 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400131 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 } value;
133 struct acpi_video_device *bind_info;
134};
135
136struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400137 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400138 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400140 u8 attached_count;
141 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400143 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500144 struct mutex device_list_lock; /* protects video_device_list */
Len Brown4be44fc2005-08-05 00:44:28 -0400145 struct proc_dir_entry *dir;
Luming Yue9dab192007-08-20 18:23:53 +0800146 struct input_dev *input;
147 char phys[32]; /* for input device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
150struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400151 u8 crt:1;
152 u8 lcd:1;
153 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500154 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400155 u8 bios:1;
156 u8 unknown:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500157 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
160struct acpi_video_device_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400161 u8 _ADR:1; /*Return the unique ID */
162 u8 _BCL:1; /*Query list of brightness control levels supported */
163 u8 _BCM:1; /*Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800164 u8 _BQC:1; /* Get current brightness level */
Len Brown4be44fc2005-08-05 00:44:28 -0400165 u8 _DDC:1; /*Return the EDID for this device */
166 u8 _DCS:1; /*Return status of output device */
167 u8 _DGS:1; /*Query graphics state */
168 u8 _DSS:1; /*Device state set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170
171struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400172 int curr;
173 int count;
174 int *levels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
177struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400178 unsigned long device_id;
179 struct acpi_video_device_flags flags;
180 struct acpi_video_device_cap cap;
181 struct list_head entry;
182 struct acpi_video_bus *video;
183 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800185 struct backlight_device *backlight;
Zhang Rui702ed512008-01-17 15:51:22 +0800186 struct thermal_cooling_device *cdev;
Luming Yu23b0f012007-05-09 21:07:05 +0800187 struct output_device *output_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/* bus */
191static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
192static struct file_operations acpi_video_bus_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700193 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400194 .open = acpi_video_bus_info_open_fs,
195 .read = seq_read,
196 .llseek = seq_lseek,
197 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
200static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
201static struct file_operations acpi_video_bus_ROM_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700202 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400203 .open = acpi_video_bus_ROM_open_fs,
204 .read = seq_read,
205 .llseek = seq_lseek,
206 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
Len Brown4be44fc2005-08-05 00:44:28 -0400209static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
210 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static struct file_operations acpi_video_bus_POST_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700212 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400213 .open = acpi_video_bus_POST_info_open_fs,
214 .read = seq_read,
215 .llseek = seq_lseek,
216 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
219static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
220static struct file_operations acpi_video_bus_POST_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700221 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400222 .open = acpi_video_bus_POST_open_fs,
223 .read = seq_read,
224 .llseek = seq_lseek,
225 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
229static struct file_operations acpi_video_bus_DOS_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700230 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400231 .open = acpi_video_bus_DOS_open_fs,
232 .read = seq_read,
233 .llseek = seq_lseek,
234 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235};
236
237/* device */
Len Brown4be44fc2005-08-05 00:44:28 -0400238static int acpi_video_device_info_open_fs(struct inode *inode,
239 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static struct file_operations acpi_video_device_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700241 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400242 .open = acpi_video_device_info_open_fs,
243 .read = seq_read,
244 .llseek = seq_lseek,
245 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246};
247
Len Brown4be44fc2005-08-05 00:44:28 -0400248static int acpi_video_device_state_open_fs(struct inode *inode,
249 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static struct file_operations acpi_video_device_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700251 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400252 .open = acpi_video_device_state_open_fs,
253 .read = seq_read,
254 .llseek = seq_lseek,
255 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
Len Brown4be44fc2005-08-05 00:44:28 -0400258static int acpi_video_device_brightness_open_fs(struct inode *inode,
259 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static struct file_operations acpi_video_device_brightness_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700261 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400262 .open = acpi_video_device_brightness_open_fs,
263 .read = seq_read,
264 .llseek = seq_lseek,
265 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266};
267
Len Brown4be44fc2005-08-05 00:44:28 -0400268static int acpi_video_device_EDID_open_fs(struct inode *inode,
269 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270static struct file_operations acpi_video_device_EDID_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700271 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400272 .open = acpi_video_device_EDID_open_fs,
273 .read = seq_read,
274 .llseek = seq_lseek,
275 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276};
277
Len Brown4be44fc2005-08-05 00:44:28 -0400278static char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 "motherboard VGA device",
280 "PCI VGA device",
281 "AGP VGA device",
282 "UNKNOWN",
283};
284
Len Brown4be44fc2005-08-05 00:44:28 -0400285static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
286static void acpi_video_device_rebind(struct acpi_video_bus *video);
287static void acpi_video_device_bind(struct acpi_video_bus *video,
288 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800290static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
291 int level);
292static int acpi_video_device_lcd_get_level_current(
293 struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400294 unsigned long long *level);
Len Brown4be44fc2005-08-05 00:44:28 -0400295static int acpi_video_get_next_level(struct acpi_video_device *device,
296 u32 level_current, u32 event);
Zhang Ruic8890f92009-03-18 16:27:08 +0800297static int acpi_video_switch_brightness(struct acpi_video_device *device,
Len Brown4be44fc2005-08-05 00:44:28 -0400298 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800299static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400300 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800301static int acpi_video_output_get(struct output_device *od);
302static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Yu Luming2f3d0002006-11-11 02:40:34 +0800304/*backlight device sysfs support*/
305static int acpi_video_get_brightness(struct backlight_device *bd)
306{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400307 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000308 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800309 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100310 (struct acpi_video_device *)bl_get_data(bd);
Zhang Ruic8890f92009-03-18 16:27:08 +0800311
312 if (acpi_video_device_lcd_get_level_current(vd, &cur_level))
313 return -EINVAL;
Matthew Garrett38531e62007-12-26 02:03:26 +0000314 for (i = 2; i < vd->brightness->count; i++) {
315 if (vd->brightness->levels[i] == cur_level)
316 /* The first two entries are special - see page 575
317 of the ACPI spec 3.0 */
318 return i-2;
319 }
320 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800321}
322
323static int acpi_video_set_brightness(struct backlight_device *bd)
324{
Matthew Garrett38531e62007-12-26 02:03:26 +0000325 int request_level = bd->props.brightness+2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800326 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100327 (struct acpi_video_device *)bl_get_data(bd);
Matthew Garrett38531e62007-12-26 02:03:26 +0000328 acpi_video_device_lcd_set_level(vd,
329 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800330 return 0;
331}
332
Richard Purdie599a52d2007-02-10 23:07:48 +0000333static struct backlight_ops acpi_backlight_ops = {
334 .get_brightness = acpi_video_get_brightness,
335 .update_status = acpi_video_set_brightness,
336};
337
Luming Yu23b0f012007-05-09 21:07:05 +0800338/*video output device sysfs support*/
339static int acpi_video_output_get(struct output_device *od)
340{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400341 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800342 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700343 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800344 acpi_video_device_get_state(vd, &state);
345 return (int)state;
346}
347
348static int acpi_video_output_set(struct output_device *od)
349{
350 unsigned long state = od->request_state;
351 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700352 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800353 return acpi_video_device_set_state(vd, state);
354}
355
356static struct output_properties acpi_output_properties = {
357 .set_state = acpi_video_output_set,
358 .get_status = acpi_video_output_get,
359};
Zhang Rui702ed512008-01-17 15:51:22 +0800360
361
362/* thermal cooling device callbacks */
363static int video_get_max_state(struct thermal_cooling_device *cdev, char *buf)
364{
365 struct acpi_device *device = cdev->devdata;
366 struct acpi_video_device *video = acpi_driver_data(device);
367
368 return sprintf(buf, "%d\n", video->brightness->count - 3);
369}
370
371static int video_get_cur_state(struct thermal_cooling_device *cdev, char *buf)
372{
373 struct acpi_device *device = cdev->devdata;
374 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400375 unsigned long long level;
Zhang Rui702ed512008-01-17 15:51:22 +0800376 int state;
377
Zhang Ruic8890f92009-03-18 16:27:08 +0800378 if (acpi_video_device_lcd_get_level_current(video, &level))
379 return -EINVAL;
Zhang Rui702ed512008-01-17 15:51:22 +0800380 for (state = 2; state < video->brightness->count; state++)
381 if (level == video->brightness->levels[state])
382 return sprintf(buf, "%d\n",
383 video->brightness->count - state - 1);
384
385 return -EINVAL;
386}
387
388static int
389video_set_cur_state(struct thermal_cooling_device *cdev, unsigned int state)
390{
391 struct acpi_device *device = cdev->devdata;
392 struct acpi_video_device *video = acpi_driver_data(device);
393 int level;
394
395 if ( state >= video->brightness->count - 2)
396 return -EINVAL;
397
398 state = video->brightness->count - state;
399 level = video->brightness->levels[state -1];
400 return acpi_video_device_lcd_set_level(video, level);
401}
402
403static struct thermal_cooling_device_ops video_cooling_ops = {
404 .get_max_state = video_get_max_state,
405 .get_cur_state = video_get_cur_state,
406 .set_cur_state = video_set_cur_state,
407};
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/* --------------------------------------------------------------------------
410 Video Management
411 -------------------------------------------------------------------------- */
412
413/* device */
414
415static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400416acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Len Brown4be44fc2005-08-05 00:44:28 -0400418 int status;
Patrick Mochel90130262006-05-19 16:54:48 -0400419
420 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Patrick Mocheld550d982006-06-27 00:41:40 -0400422 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425static int
Len Brown4be44fc2005-08-05 00:44:28 -0400426acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400427 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Len Brown4be44fc2005-08-05 00:44:28 -0400429 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Patrick Mochel90130262006-05-19 16:54:48 -0400431 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Patrick Mocheld550d982006-06-27 00:41:40 -0400433 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436static int
Len Brown4be44fc2005-08-05 00:44:28 -0400437acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Len Brown4be44fc2005-08-05 00:44:28 -0400439 int status;
440 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
441 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400442 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400446 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Patrick Mocheld550d982006-06-27 00:41:40 -0400448 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451static int
Len Brown4be44fc2005-08-05 00:44:28 -0400452acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
453 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Len Brown4be44fc2005-08-05 00:44:28 -0400455 int status;
456 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
457 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 *levels = NULL;
461
Patrick Mochel90130262006-05-19 16:54:48 -0400462 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400464 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400465 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500466 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400467 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 status = -EFAULT;
469 goto err;
470 }
471
472 *levels = obj;
473
Patrick Mocheld550d982006-06-27 00:41:40 -0400474 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Len Brown4be44fc2005-08-05 00:44:28 -0400476 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800477 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Patrick Mocheld550d982006-06-27 00:41:40 -0400479 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482static int
Len Brown4be44fc2005-08-05 00:44:28 -0400483acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400485 int status = AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400486 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
487 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800488 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400493 if (device->cap._BCM)
494 status = acpi_evaluate_object(device->dev->handle, "_BCM",
495 &args, NULL);
496 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800497 for (state = 2; state < device->brightness->count; state++)
498 if (level == device->brightness->levels[state])
499 device->backlight->props.brightness = state - 2;
500
Patrick Mocheld550d982006-06-27 00:41:40 -0400501 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504static int
Len Brown4be44fc2005-08-05 00:44:28 -0400505acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400506 unsigned long long *level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Zhang Ruic8890f92009-03-18 16:27:08 +0800508 acpi_status status = AE_OK;
509
510 if (device->cap._BQC) {
511 status = acpi_evaluate_integer(device->dev->handle, "_BQC",
512 NULL, level);
513 if (ACPI_SUCCESS(status)) {
514 device->brightness->curr = *level;
515 return 0;
516 } else {
517 /* Fixme:
518 * should we return an error or ignore this failure?
519 * dev->brightness->curr is a cached value which stores
520 * the correct current backlight level in most cases.
521 * ACPI video backlight still works w/ buggy _BQC.
522 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
523 */
524 ACPI_WARNING((AE_INFO, "Evaluating _BQC failed"));
525 device->cap._BQC = 0;
526 }
527 }
528
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400529 *level = device->brightness->curr;
Zhang Ruic8890f92009-03-18 16:27:08 +0800530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533static int
Len Brown4be44fc2005-08-05 00:44:28 -0400534acpi_video_device_EDID(struct acpi_video_device *device,
535 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Len Brown4be44fc2005-08-05 00:44:28 -0400537 int status;
538 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
539 union acpi_object *obj;
540 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
541 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 *edid = NULL;
545
546 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400547 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (length == 128)
549 arg0.integer.value = 1;
550 else if (length == 256)
551 arg0.integer.value = 2;
552 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400553 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Patrick Mochel90130262006-05-19 16:54:48 -0400555 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400557 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200559 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (obj && obj->type == ACPI_TYPE_BUFFER)
562 *edid = obj;
563 else {
Len Brown64684632006-06-26 23:41:38 -0400564 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 status = -EFAULT;
566 kfree(obj);
567 }
568
Patrick Mocheld550d982006-06-27 00:41:40 -0400569 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572/* bus */
573
574static int
Len Brown4be44fc2005-08-05 00:44:28 -0400575acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Len Brown4be44fc2005-08-05 00:44:28 -0400577 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400578 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400579 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
580 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 arg0.integer.value = option;
584
Patrick Mochel90130262006-05-19 16:54:48 -0400585 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400587 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Patrick Mocheld550d982006-06-27 00:41:40 -0400589 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400593acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 int status;
596
Patrick Mochel90130262006-05-19 16:54:48 -0400597 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Patrick Mocheld550d982006-06-27 00:41:40 -0400599 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602static int
Len Brown4be44fc2005-08-05 00:44:28 -0400603acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400604 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Len Brown4be44fc2005-08-05 00:44:28 -0400606 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Patrick Mochel90130262006-05-19 16:54:48 -0400608 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 *options &= 3;
610
Patrick Mocheld550d982006-06-27 00:41:40 -0400611 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
614/*
615 * Arg:
616 * video : video bus device pointer
617 * bios_flag :
618 * 0. The system BIOS should NOT automatically switch(toggle)
619 * the active display output.
620 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100621 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 * 2. The _DGS value should be locked.
623 * 3. The system BIOS should not automatically switch (toggle) the
624 * active display output, but instead generate the display switch
625 * event notify code.
626 * lcd_flag :
627 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100628 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100630 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * Return Value:
632 * -1 wrong arg.
633 */
634
635static int
Len Brown4be44fc2005-08-05 00:44:28 -0400636acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Len Brown4be44fc2005-08-05 00:44:28 -0400638 acpi_integer status = 0;
639 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
640 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Len Brown4be44fc2005-08-05 00:44:28 -0400643 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 status = -1;
645 goto Failed;
646 }
647 arg0.integer.value = (lcd_flag << 2) | bios_flag;
648 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400649 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Len Brown4be44fc2005-08-05 00:44:28 -0400651 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400652 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
655/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500656 * Simple comparison function used to sort backlight levels.
657 */
658
659static int
660acpi_video_cmp_level(const void *a, const void *b)
661{
662 return *(int *)a - *(int *)b;
663}
664
665/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 * Arg:
667 * device : video output device (LCD, CRT, ..)
668 *
669 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100670 * Maximum brightness level
671 *
672 * Allocate and initialize device->brightness.
673 */
674
675static int
676acpi_video_init_brightness(struct acpi_video_device *device)
677{
678 union acpi_object *obj = NULL;
679 int i, max_level = 0, count = 0;
680 union acpi_object *o;
681 struct acpi_video_device_brightness *br = NULL;
682
683 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
684 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
685 "LCD brightness level\n"));
686 goto out;
687 }
688
689 if (obj->package.count < 2)
690 goto out;
691
692 br = kzalloc(sizeof(*br), GFP_KERNEL);
693 if (!br) {
694 printk(KERN_ERR "can't allocate memory\n");
695 goto out;
696 }
697
698 br->levels = kmalloc(obj->package.count * sizeof *(br->levels),
699 GFP_KERNEL);
700 if (!br->levels)
701 goto out_free;
702
703 for (i = 0; i < obj->package.count; i++) {
704 o = (union acpi_object *)&obj->package.elements[i];
705 if (o->type != ACPI_TYPE_INTEGER) {
706 printk(KERN_ERR PREFIX "Invalid data\n");
707 continue;
708 }
709 br->levels[count] = (u32) o->integer.value;
710
711 if (br->levels[count] > max_level)
712 max_level = br->levels[count];
713 count++;
714 }
715
Zhang Rui935e5f22008-12-11 16:24:52 -0500716 /* don't sort the first two brightness levels */
717 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
718 acpi_video_cmp_level, NULL);
719
Julia Jomantaite469778c2008-06-23 22:50:42 +0100720 if (count < 2)
721 goto out_free_levels;
722
723 br->count = count;
724 device->brightness = br;
725 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "found %d brightness levels\n", count));
726 kfree(obj);
727 return max_level;
728
729out_free_levels:
730 kfree(br->levels);
731out_free:
732 kfree(br);
733out:
734 device->brightness = NULL;
735 kfree(obj);
736 return 0;
737}
738
739/*
740 * Arg:
741 * device : video output device (LCD, CRT, ..)
742 *
743 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 * None
745 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100746 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 * device.
748 */
749
Len Brown4be44fc2005-08-05 00:44:28 -0400750static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 acpi_handle h_dummy1;
Yu Luming2f3d0002006-11-11 02:40:34 +0800753 u32 max_level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
William Lee Irwin III98934de2007-12-12 03:56:55 -0800756 memset(&device->cap, 0, sizeof(device->cap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Patrick Mochel90130262006-05-19 16:54:48 -0400758 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 device->cap._ADR = 1;
760 }
Patrick Mochel90130262006-05-19 16:54:48 -0400761 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400762 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Patrick Mochel90130262006-05-19 16:54:48 -0400764 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400765 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800767 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
768 device->cap._BQC = 1;
Patrick Mochel90130262006-05-19 16:54:48 -0400769 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400770 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
Patrick Mochel90130262006-05-19 16:54:48 -0400772 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 device->cap._DCS = 1;
774 }
Patrick Mochel90130262006-05-19 16:54:48 -0400775 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 device->cap._DGS = 1;
777 }
Patrick Mochel90130262006-05-19 16:54:48 -0400778 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 device->cap._DSS = 1;
780 }
781
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200782 if (acpi_video_backlight_support())
783 max_level = acpi_video_init_brightness(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Zhao Yakuic2c78902008-07-17 10:46:05 +0800785 if (device->cap._BCL && device->cap._BCM && max_level > 0) {
Zhang Rui702ed512008-01-17 15:51:22 +0800786 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800787 static int count = 0;
788 char *name;
Yu Luming2f3d0002006-11-11 02:40:34 +0800789 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
790 if (!name)
791 return;
792
Yu Luming2f3d0002006-11-11 02:40:34 +0800793 sprintf(name, "acpi_video%d", count++);
Yu Luming2f3d0002006-11-11 02:40:34 +0800794 device->backlight = backlight_device_register(name,
Richard Purdie599a52d2007-02-10 23:07:48 +0000795 NULL, device, &acpi_backlight_ops);
Matthew Garrett38531e62007-12-26 02:03:26 +0000796 device->backlight->props.max_brightness = device->brightness->count-3;
Zhao Yakuic2c78902008-07-17 10:46:05 +0800797 /*
798 * If there exists the _BQC object, the _BQC object will be
799 * called to get the current backlight brightness. Otherwise
800 * the brightness will be set to the maximum.
801 */
802 if (device->cap._BQC)
803 device->backlight->props.brightness =
804 acpi_video_get_brightness(device->backlight);
805 else
806 device->backlight->props.brightness =
807 device->backlight->props.max_brightness;
Richard Purdie599a52d2007-02-10 23:07:48 +0000808 backlight_update_status(device->backlight);
Yu Luming2f3d0002006-11-11 02:40:34 +0800809 kfree(name);
Zhang Rui702ed512008-01-17 15:51:22 +0800810
811 device->cdev = thermal_cooling_device_register("LCD",
812 device->dev, &video_cooling_ops);
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500813 if (IS_ERR(device->cdev))
814 return;
815
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200816 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
817 device->cdev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800818 result = sysfs_create_link(&device->dev->dev.kobj,
819 &device->cdev->device.kobj,
820 "thermal_cooling");
821 if (result)
822 printk(KERN_ERR PREFIX "Create sysfs link\n");
823 result = sysfs_create_link(&device->cdev->device.kobj,
824 &device->dev->dev.kobj, "device");
825 if (result)
826 printk(KERN_ERR PREFIX "Create sysfs link\n");
827
Yu Luming2f3d0002006-11-11 02:40:34 +0800828 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200829
830 if (acpi_video_display_switch_support()) {
831
832 if (device->cap._DCS && device->cap._DSS) {
833 static int count;
834 char *name;
835 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
836 if (!name)
837 return;
838 sprintf(name, "acpi_video%d", count++);
839 device->output_dev = video_output_register(name,
840 NULL, device, &acpi_output_properties);
841 kfree(name);
842 }
Luming Yu23b0f012007-05-09 21:07:05 +0800843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
846/*
847 * Arg:
848 * device : video output device (VGA)
849 *
850 * Return Value:
851 * None
852 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100853 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 */
855
Len Brown4be44fc2005-08-05 00:44:28 -0400856static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Len Brown4be44fc2005-08-05 00:44:28 -0400858 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
William Lee Irwin III98934de2007-12-12 03:56:55 -0800860 memset(&video->cap, 0, sizeof(video->cap));
Patrick Mochel90130262006-05-19 16:54:48 -0400861 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 video->cap._DOS = 1;
863 }
Patrick Mochel90130262006-05-19 16:54:48 -0400864 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 video->cap._DOD = 1;
866 }
Patrick Mochel90130262006-05-19 16:54:48 -0400867 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 video->cap._ROM = 1;
869 }
Patrick Mochel90130262006-05-19 16:54:48 -0400870 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 video->cap._GPD = 1;
872 }
Patrick Mochel90130262006-05-19 16:54:48 -0400873 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 video->cap._SPD = 1;
875 }
Patrick Mochel90130262006-05-19 16:54:48 -0400876 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 video->cap._VPO = 1;
878 }
879}
880
881/*
882 * Check whether the video bus device has required AML method to
883 * support the desired features
884 */
885
Len Brown4be44fc2005-08-05 00:44:28 -0400886static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Len Brown4be44fc2005-08-05 00:44:28 -0400888 acpi_status status = -ENOENT;
Thomas Renninger22c13f92008-08-01 17:37:54 +0200889 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400892 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Thomas Renninger22c13f92008-08-01 17:37:54 +0200894 dev = acpi_get_physical_pci_device(video->device->handle);
895 if (!dev)
896 return -ENODEV;
897 put_device(dev);
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /* Since there is no HID, CID and so on for VGA driver, we have
900 * to check well known required nodes.
901 */
902
Julius Volz98fb8fe2007-02-20 16:38:40 +0100903 /* Does this device support video switching? */
Len Brown4be44fc2005-08-05 00:44:28 -0400904 if (video->cap._DOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 video->flags.multihead = 1;
906 status = 0;
907 }
908
Julius Volz98fb8fe2007-02-20 16:38:40 +0100909 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -0400910 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 video->flags.rom = 1;
912 status = 0;
913 }
914
Julius Volz98fb8fe2007-02-20 16:38:40 +0100915 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -0400916 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 video->flags.post = 1;
918 status = 0;
919 }
920
Patrick Mocheld550d982006-06-27 00:41:40 -0400921 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
924/* --------------------------------------------------------------------------
925 FS Interface (/proc)
926 -------------------------------------------------------------------------- */
927
Len Brown4be44fc2005-08-05 00:44:28 -0400928static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930/* video devices */
931
Len Brown4be44fc2005-08-05 00:44:28 -0400932static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200934 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 if (!dev)
938 goto end;
939
940 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
941 seq_printf(seq, "type: ");
942 if (dev->flags.crt)
943 seq_printf(seq, "CRT\n");
944 else if (dev->flags.lcd)
945 seq_printf(seq, "LCD\n");
946 else if (dev->flags.tvout)
947 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -0500948 else if (dev->flags.dvi)
949 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 else
951 seq_printf(seq, "UNKNOWN\n");
952
Len Brown4be44fc2005-08-05 00:44:28 -0400953 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Len Brown4be44fc2005-08-05 00:44:28 -0400955 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
959static int
Len Brown4be44fc2005-08-05 00:44:28 -0400960acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 return single_open(file, acpi_video_device_info_seq_show,
963 PDE(inode)->data);
964}
965
Len Brown4be44fc2005-08-05 00:44:28 -0400966static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Len Brown4be44fc2005-08-05 00:44:28 -0400968 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200969 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400970 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 if (!dev)
974 goto end;
975
976 status = acpi_video_device_get_state(dev, &state);
977 seq_printf(seq, "state: ");
978 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -0400979 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 else
981 seq_printf(seq, "<not supported>\n");
982
983 status = acpi_video_device_query(dev, &state);
984 seq_printf(seq, "query: ");
985 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -0400986 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 else
988 seq_printf(seq, "<not supported>\n");
989
Len Brown4be44fc2005-08-05 00:44:28 -0400990 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
994static int
Len Brown4be44fc2005-08-05 00:44:28 -0400995acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
997 return single_open(file, acpi_video_device_state_seq_show,
998 PDE(inode)->data);
999}
1000
1001static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001002acpi_video_device_write_state(struct file *file,
1003 const char __user * buffer,
1004 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Len Brown4be44fc2005-08-05 00:44:28 -04001006 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001007 struct seq_file *m = file->private_data;
1008 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001009 char str[12] = { 0 };
1010 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013 if (!dev || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001014 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001017 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 str[count] = 0;
1020 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001021 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 status = acpi_video_device_set_state(dev, state);
1024
1025 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001026 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Patrick Mocheld550d982006-06-27 00:41:40 -04001028 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
1031static int
Len Brown4be44fc2005-08-05 00:44:28 -04001032acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001034 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001035 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 if (!dev || !dev->brightness) {
1039 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
1043 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001044 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 seq_printf(seq, " %d", dev->brightness->levels[i]);
1046 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1047
Patrick Mocheld550d982006-06-27 00:41:40 -04001048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051static int
Len Brown4be44fc2005-08-05 00:44:28 -04001052acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
1054 return single_open(file, acpi_video_device_brightness_seq_show,
1055 PDE(inode)->data);
1056}
1057
1058static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001059acpi_video_device_write_brightness(struct file *file,
1060 const char __user * buffer,
1061 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001063 struct seq_file *m = file->private_data;
1064 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001065 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001066 unsigned int level = 0;
1067 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Thomas Renninger59d399d2005-11-08 05:27:00 -05001070 if (!dev || !dev->brightness || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001071 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001074 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 str[count] = 0;
1077 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001080 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Julius Volz98fb8fe2007-02-20 16:38:40 +01001082 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001083 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (level == dev->brightness->levels[i]) {
Len Brown4be44fc2005-08-05 00:44:28 -04001085 if (ACPI_SUCCESS
1086 (acpi_video_device_lcd_set_level(dev, level)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 dev->brightness->curr = level;
1088 break;
1089 }
1090
Patrick Mocheld550d982006-06-27 00:41:40 -04001091 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092}
1093
Len Brown4be44fc2005-08-05 00:44:28 -04001094static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001096 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001097 int status;
1098 int i;
1099 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 if (!dev)
1103 goto out;
1104
Len Brown4be44fc2005-08-05 00:44:28 -04001105 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001107 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109
1110 if (ACPI_FAILURE(status)) {
1111 goto out;
1112 }
1113
1114 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1115 for (i = 0; i < edid->buffer.length; i++)
1116 seq_putc(seq, edid->buffer.pointer[i]);
1117 }
1118
Len Brown4be44fc2005-08-05 00:44:28 -04001119 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 if (!edid)
1121 seq_printf(seq, "<not supported>\n");
1122 else
1123 kfree(edid);
1124
Patrick Mocheld550d982006-06-27 00:41:40 -04001125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
1128static int
Len Brown4be44fc2005-08-05 00:44:28 -04001129acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 return single_open(file, acpi_video_device_EDID_seq_show,
1132 PDE(inode)->data);
1133}
1134
Len Brown4be44fc2005-08-05 00:44:28 -04001135static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001137 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 struct acpi_video_device *vid_dev;
1139
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001140 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001142 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001144 device_dir = proc_mkdir(acpi_device_bid(device),
1145 vid_dev->video->dir);
1146 if (!device_dir)
1147 return -ENOMEM;
1148
1149 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001152 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001153 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001155 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001158 acpi_video_device_state_fops.write = acpi_video_device_write_state;
1159 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001160 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001161 &acpi_video_device_state_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_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001167 acpi_video_device_brightness_fops.write =
1168 acpi_video_device_write_brightness;
1169 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001170 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001171 &acpi_video_device_brightness_fops,
1172 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001174 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001177 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001178 &acpi_video_device_EDID_fops,
1179 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001181 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001183 acpi_device_dir(device) = device_dir;
1184
Patrick Mocheld550d982006-06-27 00:41:40 -04001185 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001186
1187 err_remove_brightness:
1188 remove_proc_entry("brightness", device_dir);
1189 err_remove_state:
1190 remove_proc_entry("state", device_dir);
1191 err_remove_info:
1192 remove_proc_entry("info", device_dir);
1193 err_remove_dir:
1194 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1195 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
Len Brown4be44fc2005-08-05 00:44:28 -04001198static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001201 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001203 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001205 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001207 device_dir = acpi_device_dir(device);
1208 if (device_dir) {
1209 remove_proc_entry("info", device_dir);
1210 remove_proc_entry("state", device_dir);
1211 remove_proc_entry("brightness", device_dir);
1212 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001213 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 acpi_device_dir(device) = NULL;
1215 }
1216
Patrick Mocheld550d982006-06-27 00:41:40 -04001217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001221static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001223 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 if (!video)
1227 goto end;
1228
1229 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001230 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001232 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001234 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Len Brown4be44fc2005-08-05 00:44:28 -04001236 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Len Brown4be44fc2005-08-05 00:44:28 -04001240static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Len Brown4be44fc2005-08-05 00:44:28 -04001242 return single_open(file, acpi_video_bus_info_seq_show,
1243 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
1245
Len Brown4be44fc2005-08-05 00:44:28 -04001246static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001248 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 if (!video)
1252 goto end;
1253
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001254 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 seq_printf(seq, "<TODO>\n");
1256
Len Brown4be44fc2005-08-05 00:44:28 -04001257 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
Len Brown4be44fc2005-08-05 00:44:28 -04001261static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1264}
1265
Len Brown4be44fc2005-08-05 00:44:28 -04001266static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001268 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001269 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001270 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 if (!video)
1274 goto end;
1275
1276 status = acpi_video_bus_POST_options(video, &options);
1277 if (ACPI_SUCCESS(status)) {
1278 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001279 printk(KERN_WARNING PREFIX
1280 "The motherboard VGA device is not listed as a possible POST device.\n");
1281 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001282 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
Frank Seidel4d939152009-02-04 17:03:07 +01001284 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001285 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (options & 2)
1287 seq_printf(seq, " <PCI video>");
1288 if (options & 4)
1289 seq_printf(seq, " <AGP video>");
1290 seq_putc(seq, '\n');
1291 } else
1292 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001293 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
1297static int
Len Brown4be44fc2005-08-05 00:44:28 -04001298acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Len Brown4be44fc2005-08-05 00:44:28 -04001300 return single_open(file, acpi_video_bus_POST_info_seq_show,
1301 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
Len Brown4be44fc2005-08-05 00:44:28 -04001304static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001306 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001307 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001308 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 if (!video)
1312 goto end;
1313
Len Brown4be44fc2005-08-05 00:44:28 -04001314 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 if (!ACPI_SUCCESS(status)) {
1316 seq_printf(seq, "<not supported>\n");
1317 goto end;
1318 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001319 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Len Brown4be44fc2005-08-05 00:44:28 -04001321 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
Len Brown4be44fc2005-08-05 00:44:28 -04001325static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001327 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Len Brown4be44fc2005-08-05 00:44:28 -04001330 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Patrick Mocheld550d982006-06-27 00:41:40 -04001332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Len Brown4be44fc2005-08-05 00:44:28 -04001335static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Len Brown4be44fc2005-08-05 00:44:28 -04001337 return single_open(file, acpi_video_bus_POST_seq_show,
1338 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
Len Brown4be44fc2005-08-05 00:44:28 -04001341static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1344}
1345
1346static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001347acpi_video_bus_write_POST(struct file *file,
1348 const char __user * buffer,
1349 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Len Brown4be44fc2005-08-05 00:44:28 -04001351 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001352 struct seq_file *m = file->private_data;
1353 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001354 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001355 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 status = acpi_video_bus_POST_options(video, &options);
1362 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001363 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001366 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 str[count] = 0;
1369 opt = strtoul(str, NULL, 0);
1370 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001371 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Julius Volz98fb8fe2007-02-20 16:38:40 +01001373 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 options |= 1;
1375
1376 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001377 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001379 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 }
1382
Patrick Mocheld550d982006-06-27 00:41:40 -04001383 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001387acpi_video_bus_write_DOS(struct file *file,
1388 const char __user * buffer,
1389 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Len Brown4be44fc2005-08-05 00:44:28 -04001391 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001392 struct seq_file *m = file->private_data;
1393 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001394 char str[12] = { 0 };
1395 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001399 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001402 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 str[count] = 0;
1405 opt = strtoul(str, NULL, 0);
1406 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001407 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Len Brown4be44fc2005-08-05 00:44:28 -04001409 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001412 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Patrick Mocheld550d982006-06-27 00:41:40 -04001414 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Len Brown4be44fc2005-08-05 00:44:28 -04001417static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001419 struct acpi_video_bus *video = acpi_driver_data(device);
1420 struct proc_dir_entry *device_dir;
1421 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001423 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1424 if (!device_dir)
1425 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001427 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001430 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001431 &acpi_video_bus_info_fops,
1432 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001434 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001437 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001438 &acpi_video_bus_ROM_fops,
1439 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001441 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001444 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001445 &acpi_video_bus_POST_info_fops,
1446 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001448 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 /* 'POST' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001451 acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001452 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001453 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001454 &acpi_video_bus_POST_fops,
1455 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001457 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 /* 'DOS' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001460 acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001461 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001462 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001463 &acpi_video_bus_DOS_fops,
1464 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001466 goto err_remove_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001468 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001469 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001470
1471 err_remove_post:
1472 remove_proc_entry("POST", device_dir);
1473 err_remove_post_info:
1474 remove_proc_entry("POST_info", device_dir);
1475 err_remove_rom:
1476 remove_proc_entry("ROM", device_dir);
1477 err_remove_info:
1478 remove_proc_entry("info", device_dir);
1479 err_remove_dir:
1480 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1481 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Len Brown4be44fc2005-08-05 00:44:28 -04001484static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001486 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001488 if (device_dir) {
1489 remove_proc_entry("info", device_dir);
1490 remove_proc_entry("ROM", device_dir);
1491 remove_proc_entry("POST_info", device_dir);
1492 remove_proc_entry("POST", device_dir);
1493 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001494 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 acpi_device_dir(device) = NULL;
1496 }
1497
Patrick Mocheld550d982006-06-27 00:41:40 -04001498 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
1501/* --------------------------------------------------------------------------
1502 Driver Interface
1503 -------------------------------------------------------------------------- */
1504
1505/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001506static struct acpi_video_device_attrib*
1507acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1508{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001509 struct acpi_video_enumerated_device *ids;
1510 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001511
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001512 for (i = 0; i < video->attached_count; i++) {
1513 ids = &video->attached_array[i];
1514 if ((ids->value.int_val & 0xffff) == device_id)
1515 return &ids->value.attrib;
1516 }
1517
Rui Zhang82cae992007-01-03 23:40:53 -05001518 return NULL;
1519}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521static int
Len Brown4be44fc2005-08-05 00:44:28 -04001522acpi_video_bus_get_one_device(struct acpi_device *device,
1523 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001525 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001526 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001527 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001528 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001531 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Len Brown4be44fc2005-08-05 00:44:28 -04001533 status =
1534 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (ACPI_SUCCESS(status)) {
1536
Burman Yan36bcbec2006-12-19 12:56:11 -08001537 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001539 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1542 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001543 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 data->device_id = device_id;
1546 data->video = video;
1547 data->dev = device;
1548
Rui Zhang82cae992007-01-03 23:40:53 -05001549 attribute = acpi_video_get_device_attr(video, device_id);
1550
1551 if((attribute != NULL) && attribute->device_id_scheme) {
1552 switch (attribute->display_type) {
1553 case ACPI_VIDEO_DISPLAY_CRT:
1554 data->flags.crt = 1;
1555 break;
1556 case ACPI_VIDEO_DISPLAY_TV:
1557 data->flags.tvout = 1;
1558 break;
1559 case ACPI_VIDEO_DISPLAY_DVI:
1560 data->flags.dvi = 1;
1561 break;
1562 case ACPI_VIDEO_DISPLAY_LCD:
1563 data->flags.lcd = 1;
1564 break;
1565 default:
1566 data->flags.unknown = 1;
1567 break;
1568 }
1569 if(attribute->bios_can_detect)
1570 data->flags.bios = 1;
1571 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 acpi_video_device_bind(video, data);
1575 acpi_video_device_find_cap(data);
1576
Patrick Mochel90130262006-05-19 16:54:48 -04001577 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001578 ACPI_DEVICE_NOTIFY,
1579 acpi_video_device_notify,
1580 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001582 printk(KERN_ERR PREFIX
1583 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001584 if(data->brightness)
1585 kfree(data->brightness->levels);
1586 kfree(data->brightness);
1587 kfree(data);
1588 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
1590
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001591 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001593 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 acpi_video_device_add_fs(device);
1596
Patrick Mocheld550d982006-06-27 00:41:40 -04001597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599
Patrick Mocheld550d982006-06-27 00:41:40 -04001600 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602
1603/*
1604 * Arg:
1605 * video : video bus device
1606 *
1607 * Return:
1608 * none
1609 *
1610 * Enumerate the video device list of the video bus,
1611 * bind the ids with the corresponding video devices
1612 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001613 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Len Brown4be44fc2005-08-05 00:44:28 -04001615static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001617 struct acpi_video_device *dev;
1618
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001619 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001620
1621 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001622 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001623
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001624 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
1627/*
1628 * Arg:
1629 * video : video bus device
1630 * device : video output device under the video
1631 * bus
1632 *
1633 * Return:
1634 * none
1635 *
1636 * Bind the ids with the corresponding video devices
1637 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001638 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640static void
Len Brown4be44fc2005-08-05 00:44:28 -04001641acpi_video_device_bind(struct acpi_video_bus *video,
1642 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001644 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001645 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001647 for (i = 0; i < video->attached_count; i++) {
1648 ids = &video->attached_array[i];
1649 if (device->device_id == (ids->value.int_val & 0xffff)) {
1650 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1652 }
1653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
1656/*
1657 * Arg:
1658 * video : video bus device
1659 *
1660 * Return:
1661 * < 0 : error
1662 *
1663 * Call _DOD to enumerate all devices attached to display adapter
1664 *
Len Brown4be44fc2005-08-05 00:44:28 -04001665 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1668{
Len Brown4be44fc2005-08-05 00:44:28 -04001669 int status;
1670 int count;
1671 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001672 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001673 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1674 union acpi_object *dod = NULL;
1675 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Patrick Mochel90130262006-05-19 16:54:48 -04001677 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001679 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001680 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001683 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001685 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 status = -EFAULT;
1687 goto out;
1688 }
1689
1690 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001691 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001693 active_list = kcalloc(1 + dod->package.count,
1694 sizeof(struct acpi_video_enumerated_device),
1695 GFP_KERNEL);
1696 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 status = -ENOMEM;
1698 goto out;
1699 }
1700
1701 count = 0;
1702 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001703 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001706 printk(KERN_ERR PREFIX
1707 "Invalid _DOD data in element %d\n", i);
1708 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001710
1711 active_list[count].value.int_val = obj->integer.value;
1712 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001713 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1714 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 count++;
1716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Jesper Juhl6044ec82005-11-07 01:01:32 -08001718 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001719
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001720 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001722
1723 out:
Len Brown02438d82006-06-30 03:19:10 -04001724 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001725 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
Len Brown4be44fc2005-08-05 00:44:28 -04001728static int
1729acpi_video_get_next_level(struct acpi_video_device *device,
1730 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001732 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001733 max = max_below = 0;
1734 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001735 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001736 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001737 l = device->brightness->levels[i];
1738 if (abs(l - level_current) < abs(delta)) {
1739 delta = l - level_current;
1740 if (!delta)
1741 break;
1742 }
1743 }
1744 /* Ajust level_current to closest available level */
1745 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001746 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001747 l = device->brightness->levels[i];
1748 if (l < min)
1749 min = l;
1750 if (l > max)
1751 max = l;
1752 if (l < min_above && l > level_current)
1753 min_above = l;
1754 if (l > max_below && l < level_current)
1755 max_below = l;
1756 }
1757
1758 switch (event) {
1759 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1760 return (level_current < max) ? min_above : min;
1761 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1762 return (level_current < max) ? min_above : max;
1763 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1764 return (level_current > min) ? max_below : min;
1765 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1766 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1767 return 0;
1768 default:
1769 return level_current;
1770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
Zhang Ruic8890f92009-03-18 16:27:08 +08001773static int
Len Brown4be44fc2005-08-05 00:44:28 -04001774acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001776 unsigned long long level_current, level_next;
Zhang Ruic8890f92009-03-18 16:27:08 +08001777 int result = -EINVAL;
1778
Julia Jomantaite469778c2008-06-23 22:50:42 +01001779 if (!device->brightness)
Zhang Ruic8890f92009-03-18 16:27:08 +08001780 goto out;
1781
1782 result = acpi_video_device_lcd_get_level_current(device,
1783 &level_current);
1784 if (result)
1785 goto out;
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 level_next = acpi_video_get_next_level(device, level_current, event);
Zhang Ruic8890f92009-03-18 16:27:08 +08001788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 acpi_video_device_lcd_set_level(device, level_next);
Zhang Ruic8890f92009-03-18 16:27:08 +08001790
1791out:
1792 if (result)
1793 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1794
1795 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
1798static int
Len Brown4be44fc2005-08-05 00:44:28 -04001799acpi_video_bus_get_devices(struct acpi_video_bus *video,
1800 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Len Brown4be44fc2005-08-05 00:44:28 -04001802 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001803 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 acpi_video_device_enumerate(video);
1806
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001807 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 status = acpi_video_bus_get_one_device(dev, video);
1810 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001811 printk(KERN_WARNING PREFIX
1812 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 continue;
1814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001816 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Len Brown4be44fc2005-08-05 00:44:28 -04001819static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Karol Kozimor031ec772005-07-30 04:18:00 -04001821 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 struct acpi_video_bus *video;
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001826 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 video = device->video;
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 acpi_video_device_remove_fs(device->dev);
1831
Patrick Mochel90130262006-05-19 16:54:48 -04001832 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001833 ACPI_DEVICE_NOTIFY,
1834 acpi_video_device_notify);
Richard Purdie599a52d2007-02-10 23:07:48 +00001835 backlight_device_unregister(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +08001836 if (device->cdev) {
1837 sysfs_remove_link(&device->dev->dev.kobj,
1838 "thermal_cooling");
1839 sysfs_remove_link(&device->cdev->device.kobj,
1840 "device");
1841 thermal_cooling_device_unregister(device->cdev);
1842 device->cdev = NULL;
1843 }
Luming Yu23b0f012007-05-09 21:07:05 +08001844 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001845
Patrick Mocheld550d982006-06-27 00:41:40 -04001846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
Len Brown4be44fc2005-08-05 00:44:28 -04001849static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Len Brown4be44fc2005-08-05 00:44:28 -04001851 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001852 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001854 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001856 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001858 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04001859 if (ACPI_FAILURE(status))
1860 printk(KERN_WARNING PREFIX
1861 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001863 if (dev->brightness) {
1864 kfree(dev->brightness->levels);
1865 kfree(dev->brightness);
1866 }
1867 list_del(&dev->entry);
1868 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 }
1870
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001871 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001872
Patrick Mocheld550d982006-06-27 00:41:40 -04001873 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
1875
1876/* acpi_video interface */
1877
Len Brown4be44fc2005-08-05 00:44:28 -04001878static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Zhang Ruia21101c2007-09-14 11:46:22 +08001880 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Len Brown4be44fc2005-08-05 00:44:28 -04001883static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
1885 return acpi_video_bus_DOS(video, 0, 1);
1886}
1887
Len Brown4be44fc2005-08-05 00:44:28 -04001888static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001890 struct acpi_video_bus *video = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001891 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001892 struct input_dev *input;
1893 int keycode;
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001896 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001898 device = video->device;
Luming Yue9dab192007-08-20 18:23:53 +08001899 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001902 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04001904 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001905 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 break;
1907
Julius Volz98fb8fe2007-02-20 16:38:40 +01001908 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 * connector. */
1910 acpi_video_device_enumerate(video);
1911 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04001912 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001913 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 break;
1915
Len Brown4be44fc2005-08-05 00:44:28 -04001916 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001917 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001918 keycode = KEY_SWITCHVIDEOMODE;
1919 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001920 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001921 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001922 keycode = KEY_VIDEO_NEXT;
1923 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001924 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04001925 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001926 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 break;
1928
1929 default:
Luming Yue9dab192007-08-20 18:23:53 +08001930 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001932 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 break;
1934 }
1935
Zhang Rui7761f632008-01-25 14:48:12 +08001936 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001937 input_report_key(input, keycode, 1);
1938 input_sync(input);
1939 input_report_key(input, keycode, 0);
1940 input_sync(input);
1941
Patrick Mocheld550d982006-06-27 00:41:40 -04001942 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
Len Brown4be44fc2005-08-05 00:44:28 -04001945static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001947 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001948 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001949 struct acpi_video_bus *bus;
1950 struct input_dev *input;
1951 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001954 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001956 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001957 bus = video_device->video;
1958 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001961 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001962 if (brightness_switch_enabled)
1963 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001964 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001965 keycode = KEY_BRIGHTNESS_CYCLE;
1966 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001967 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001968 if (brightness_switch_enabled)
1969 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001970 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001971 keycode = KEY_BRIGHTNESSUP;
1972 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001973 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001974 if (brightness_switch_enabled)
1975 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001976 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001977 keycode = KEY_BRIGHTNESSDOWN;
1978 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001979 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08001980 if (brightness_switch_enabled)
1981 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001982 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001983 keycode = KEY_BRIGHTNESS_ZERO;
1984 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001985 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08001986 if (brightness_switch_enabled)
1987 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04001988 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001989 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 break;
1991 default:
Luming Yue9dab192007-08-20 18:23:53 +08001992 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001994 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 break;
1996 }
Luming Yue9dab192007-08-20 18:23:53 +08001997
Zhang Rui7761f632008-01-25 14:48:12 +08001998 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001999 input_report_key(input, keycode, 1);
2000 input_sync(input);
2001 input_report_key(input, keycode, 0);
2002 input_sync(input);
2003
Patrick Mocheld550d982006-06-27 00:41:40 -04002004 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
Zhang Ruie6d9da12007-08-25 02:23:31 -04002007static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08002008static int acpi_video_resume(struct acpi_device *device)
2009{
2010 struct acpi_video_bus *video;
2011 struct acpi_video_device *video_device;
2012 int i;
2013
2014 if (!device || !acpi_driver_data(device))
2015 return -EINVAL;
2016
2017 video = acpi_driver_data(device);
2018
2019 for (i = 0; i < video->attached_count; i++) {
2020 video_device = video->attached_array[i].bind_info;
2021 if (video_device && video_device->backlight)
2022 acpi_video_set_brightness(video_device->backlight);
2023 }
2024 return AE_OK;
2025}
2026
Len Brown4be44fc2005-08-05 00:44:28 -04002027static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002029 acpi_status status;
2030 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002031 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002032 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Burman Yan36bcbec2006-12-19 12:56:11 -08002034 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002036 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Zhang Ruie6d9da12007-08-25 02:23:31 -04002038 /* a hack to fix the duplicate name "VID" problem on T61 */
2039 if (!strcmp(device->pnp.bus_id, "VID")) {
2040 if (instance)
2041 device->pnp.bus_id[3] = '0' + instance;
2042 instance ++;
2043 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002044 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2045 if (!strcmp(device->pnp.bus_id, "VGA")) {
2046 if (instance)
2047 device->pnp.bus_id[3] = '0' + instance;
2048 instance++;
2049 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002050
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002051 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2053 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002054 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002057 error = acpi_video_bus_check(video);
2058 if (error)
2059 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002061 error = acpi_video_bus_add_fs(device);
2062 if (error)
2063 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002065 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 INIT_LIST_HEAD(&video->video_device_list);
2067
2068 acpi_video_bus_get_devices(video, device);
2069 acpi_video_bus_start_devices(video);
2070
Patrick Mochel90130262006-05-19 16:54:48 -04002071 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002072 ACPI_DEVICE_NOTIFY,
2073 acpi_video_bus_notify, video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002075 printk(KERN_ERR PREFIX
2076 "Error installing notify handler\n");
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002077 error = -ENODEV;
2078 goto err_stop_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
2080
Luming Yue9dab192007-08-20 18:23:53 +08002081 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002082 if (!input) {
2083 error = -ENOMEM;
2084 goto err_uninstall_notify;
2085 }
Luming Yue9dab192007-08-20 18:23:53 +08002086
2087 snprintf(video->phys, sizeof(video->phys),
2088 "%s/video/input0", acpi_device_hid(video->device));
2089
2090 input->name = acpi_device_name(video->device);
2091 input->phys = video->phys;
2092 input->id.bustype = BUS_HOST;
2093 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002094 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002095 input->evbit[0] = BIT(EV_KEY);
2096 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2097 set_bit(KEY_VIDEO_NEXT, input->keybit);
2098 set_bit(KEY_VIDEO_PREV, input->keybit);
2099 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2100 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2101 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2102 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2103 set_bit(KEY_DISPLAY_OFF, input->keybit);
2104 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002105
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002106 error = input_register_device(input);
2107 if (error)
2108 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002111 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2112 video->flags.multihead ? "yes" : "no",
2113 video->flags.rom ? "yes" : "no",
2114 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002118 err_free_input_dev:
2119 input_free_device(input);
2120 err_uninstall_notify:
2121 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
2122 acpi_video_bus_notify);
2123 err_stop_video:
2124 acpi_video_bus_stop_devices(video);
2125 acpi_video_bus_put_devices(video);
2126 kfree(video->attached_array);
2127 acpi_video_bus_remove_fs(device);
2128 err_free_video:
2129 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002130 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002131
2132 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
Len Brown4be44fc2005-08-05 00:44:28 -04002135static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Len Brown4be44fc2005-08-05 00:44:28 -04002137 acpi_status status = 0;
2138 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002142 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002144 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 acpi_video_bus_stop_devices(video);
2147
Patrick Mochel90130262006-05-19 16:54:48 -04002148 status = acpi_remove_notify_handler(video->device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002149 ACPI_DEVICE_NOTIFY,
2150 acpi_video_bus_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 acpi_video_bus_put_devices(video);
2153 acpi_video_bus_remove_fs(device);
2154
Luming Yue9dab192007-08-20 18:23:53 +08002155 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002156 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 kfree(video);
2158
Patrick Mocheld550d982006-06-27 00:41:40 -04002159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
Len Brown4be44fc2005-08-05 00:44:28 -04002162static int __init acpi_video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Len Brown4be44fc2005-08-05 00:44:28 -04002164 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2167 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002168 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 acpi_video_dir->owner = THIS_MODULE;
2170
2171 result = acpi_bus_register_driver(&acpi_video_bus);
2172 if (result < 0) {
2173 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002174 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
2176
Patrick Mocheld550d982006-06-27 00:41:40 -04002177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
Len Brown4be44fc2005-08-05 00:44:28 -04002180static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 acpi_bus_unregister_driver(&acpi_video_bus);
2184
2185 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2186
Patrick Mocheld550d982006-06-27 00:41:40 -04002187 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
2190module_init(acpi_video_init);
2191module_exit(acpi_video_exit);