blob: 0e7e689634a79e7c0eca28c4a4576791d259676b [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);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100192static const 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);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100201static const 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);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100211static const 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);
Len Brownc07c9a72009-04-04 03:33:45 -0400220static ssize_t acpi_video_bus_write_POST(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100221 const char __user *buffer, size_t count, loff_t *data);
222static const struct file_operations acpi_video_bus_POST_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700223 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400224 .open = acpi_video_bus_POST_open_fs,
225 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100226 .write = acpi_video_bus_write_POST,
Len Brown4be44fc2005-08-05 00:44:28 -0400227 .llseek = seq_lseek,
228 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229};
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400232static ssize_t acpi_video_bus_write_DOS(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100233 const char __user *buffer, size_t count, loff_t *data);
234static const struct file_operations acpi_video_bus_DOS_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700235 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400236 .open = acpi_video_bus_DOS_open_fs,
237 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100238 .write = acpi_video_bus_write_DOS,
Len Brown4be44fc2005-08-05 00:44:28 -0400239 .llseek = seq_lseek,
240 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241};
242
243/* device */
Len Brown4be44fc2005-08-05 00:44:28 -0400244static int acpi_video_device_info_open_fs(struct inode *inode,
245 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100246static const struct file_operations acpi_video_device_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700247 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400248 .open = acpi_video_device_info_open_fs,
249 .read = seq_read,
250 .llseek = seq_lseek,
251 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252};
253
Len Brown4be44fc2005-08-05 00:44:28 -0400254static int acpi_video_device_state_open_fs(struct inode *inode,
255 struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400256static ssize_t acpi_video_device_write_state(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100257 const char __user *buffer, size_t count, loff_t *data);
258static const struct file_operations acpi_video_device_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700259 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400260 .open = acpi_video_device_state_open_fs,
261 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100262 .write = acpi_video_device_write_state,
Len Brown4be44fc2005-08-05 00:44:28 -0400263 .llseek = seq_lseek,
264 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265};
266
Len Brown4be44fc2005-08-05 00:44:28 -0400267static int acpi_video_device_brightness_open_fs(struct inode *inode,
268 struct file *file);
Len Brownc07c9a72009-04-04 03:33:45 -0400269static ssize_t acpi_video_device_write_brightness(struct file *file,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100270 const char __user *buffer, size_t count, loff_t *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271static struct file_operations acpi_video_device_brightness_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700272 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400273 .open = acpi_video_device_brightness_open_fs,
274 .read = seq_read,
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100275 .write = acpi_video_device_write_brightness,
Len Brown4be44fc2005-08-05 00:44:28 -0400276 .llseek = seq_lseek,
277 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278};
279
Len Brown4be44fc2005-08-05 00:44:28 -0400280static int acpi_video_device_EDID_open_fs(struct inode *inode,
281 struct file *file);
Jan Engelhardt070d8eb2009-01-12 00:07:55 +0100282static const struct file_operations acpi_video_device_EDID_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700283 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400284 .open = acpi_video_device_EDID_open_fs,
285 .read = seq_read,
286 .llseek = seq_lseek,
287 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288};
289
Jan Engelhardtb7171ae2009-01-12 00:08:19 +0100290static const char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 "motherboard VGA device",
292 "PCI VGA device",
293 "AGP VGA device",
294 "UNKNOWN",
295};
296
Len Brown4be44fc2005-08-05 00:44:28 -0400297static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
298static void acpi_video_device_rebind(struct acpi_video_bus *video);
299static void acpi_video_device_bind(struct acpi_video_bus *video,
300 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800302static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
303 int level);
304static int acpi_video_device_lcd_get_level_current(
305 struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400306 unsigned long long *level);
Len Brown4be44fc2005-08-05 00:44:28 -0400307static int acpi_video_get_next_level(struct acpi_video_device *device,
308 u32 level_current, u32 event);
309static void acpi_video_switch_brightness(struct acpi_video_device *device,
310 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800311static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400312 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800313static int acpi_video_output_get(struct output_device *od);
314static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Yu Luming2f3d0002006-11-11 02:40:34 +0800316/*backlight device sysfs support*/
317static int acpi_video_get_brightness(struct backlight_device *bd)
318{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400319 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000320 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800321 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100322 (struct acpi_video_device *)bl_get_data(bd);
Yu Luming2f3d0002006-11-11 02:40:34 +0800323 acpi_video_device_lcd_get_level_current(vd, &cur_level);
Matthew Garrett38531e62007-12-26 02:03:26 +0000324 for (i = 2; i < vd->brightness->count; i++) {
325 if (vd->brightness->levels[i] == cur_level)
326 /* The first two entries are special - see page 575
327 of the ACPI spec 3.0 */
328 return i-2;
329 }
330 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800331}
332
333static int acpi_video_set_brightness(struct backlight_device *bd)
334{
Matthew Garrett38531e62007-12-26 02:03:26 +0000335 int request_level = bd->props.brightness+2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800336 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100337 (struct acpi_video_device *)bl_get_data(bd);
Matthew Garrett38531e62007-12-26 02:03:26 +0000338 acpi_video_device_lcd_set_level(vd,
339 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800340 return 0;
341}
342
Richard Purdie599a52d2007-02-10 23:07:48 +0000343static struct backlight_ops acpi_backlight_ops = {
344 .get_brightness = acpi_video_get_brightness,
345 .update_status = acpi_video_set_brightness,
346};
347
Luming Yu23b0f012007-05-09 21:07:05 +0800348/*video output device sysfs support*/
349static int acpi_video_output_get(struct output_device *od)
350{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400351 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800352 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700353 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800354 acpi_video_device_get_state(vd, &state);
355 return (int)state;
356}
357
358static int acpi_video_output_set(struct output_device *od)
359{
360 unsigned long state = od->request_state;
361 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700362 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800363 return acpi_video_device_set_state(vd, state);
364}
365
366static struct output_properties acpi_output_properties = {
367 .set_state = acpi_video_output_set,
368 .get_status = acpi_video_output_get,
369};
Zhang Rui702ed512008-01-17 15:51:22 +0800370
371
372/* thermal cooling device callbacks */
373static int video_get_max_state(struct thermal_cooling_device *cdev, char *buf)
374{
375 struct acpi_device *device = cdev->devdata;
376 struct acpi_video_device *video = acpi_driver_data(device);
377
378 return sprintf(buf, "%d\n", video->brightness->count - 3);
379}
380
381static int video_get_cur_state(struct thermal_cooling_device *cdev, char *buf)
382{
383 struct acpi_device *device = cdev->devdata;
384 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400385 unsigned long long level;
Zhang Rui702ed512008-01-17 15:51:22 +0800386 int state;
387
388 acpi_video_device_lcd_get_level_current(video, &level);
389 for (state = 2; state < video->brightness->count; state++)
390 if (level == video->brightness->levels[state])
391 return sprintf(buf, "%d\n",
392 video->brightness->count - state - 1);
393
394 return -EINVAL;
395}
396
397static int
398video_set_cur_state(struct thermal_cooling_device *cdev, unsigned int state)
399{
400 struct acpi_device *device = cdev->devdata;
401 struct acpi_video_device *video = acpi_driver_data(device);
402 int level;
403
404 if ( state >= video->brightness->count - 2)
405 return -EINVAL;
406
407 state = video->brightness->count - state;
408 level = video->brightness->levels[state -1];
409 return acpi_video_device_lcd_set_level(video, level);
410}
411
412static struct thermal_cooling_device_ops video_cooling_ops = {
413 .get_max_state = video_get_max_state,
414 .get_cur_state = video_get_cur_state,
415 .set_cur_state = video_set_cur_state,
416};
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/* --------------------------------------------------------------------------
419 Video Management
420 -------------------------------------------------------------------------- */
421
422/* device */
423
424static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400425acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Len Brown4be44fc2005-08-05 00:44:28 -0400427 int status;
Patrick Mochel90130262006-05-19 16:54:48 -0400428
429 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Patrick Mocheld550d982006-06-27 00:41:40 -0400431 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434static int
Len Brown4be44fc2005-08-05 00:44:28 -0400435acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400436 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Len Brown4be44fc2005-08-05 00:44:28 -0400438 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Patrick Mochel90130262006-05-19 16:54:48 -0400440 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Patrick Mocheld550d982006-06-27 00:41:40 -0400442 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
445static int
Len Brown4be44fc2005-08-05 00:44:28 -0400446acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Len Brown4be44fc2005-08-05 00:44:28 -0400448 int status;
449 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
450 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400451 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400455 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Patrick Mocheld550d982006-06-27 00:41:40 -0400457 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460static int
Len Brown4be44fc2005-08-05 00:44:28 -0400461acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
462 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Len Brown4be44fc2005-08-05 00:44:28 -0400464 int status;
465 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
466 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 *levels = NULL;
470
Patrick Mochel90130262006-05-19 16:54:48 -0400471 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400473 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400474 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500475 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400476 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 status = -EFAULT;
478 goto err;
479 }
480
481 *levels = obj;
482
Patrick Mocheld550d982006-06-27 00:41:40 -0400483 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Len Brown4be44fc2005-08-05 00:44:28 -0400485 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800486 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Patrick Mocheld550d982006-06-27 00:41:40 -0400488 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491static int
Len Brown4be44fc2005-08-05 00:44:28 -0400492acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400494 int status = AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400495 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
496 struct acpi_object_list args = { 1, &arg0 };
Zhang Rui9e6dada2008-12-31 10:58:48 +0800497 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400502 if (device->cap._BCM)
503 status = acpi_evaluate_object(device->dev->handle, "_BCM",
504 &args, NULL);
505 device->brightness->curr = level;
Zhang Rui9e6dada2008-12-31 10:58:48 +0800506 for (state = 2; state < device->brightness->count; state++)
507 if (level == device->brightness->levels[state])
508 device->backlight->props.brightness = state - 2;
509
Patrick Mocheld550d982006-06-27 00:41:40 -0400510 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513static int
Len Brown4be44fc2005-08-05 00:44:28 -0400514acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400515 unsigned long long *level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400517 if (device->cap._BQC)
518 return acpi_evaluate_integer(device->dev->handle, "_BQC", NULL,
519 level);
520 *level = device->brightness->curr;
521 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
523
524static int
Len Brown4be44fc2005-08-05 00:44:28 -0400525acpi_video_device_EDID(struct acpi_video_device *device,
526 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Len Brown4be44fc2005-08-05 00:44:28 -0400528 int status;
529 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
530 union acpi_object *obj;
531 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
532 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 *edid = NULL;
536
537 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400538 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (length == 128)
540 arg0.integer.value = 1;
541 else if (length == 256)
542 arg0.integer.value = 2;
543 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400544 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Patrick Mochel90130262006-05-19 16:54:48 -0400546 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400548 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200550 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 if (obj && obj->type == ACPI_TYPE_BUFFER)
553 *edid = obj;
554 else {
Len Brown64684632006-06-26 23:41:38 -0400555 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 status = -EFAULT;
557 kfree(obj);
558 }
559
Patrick Mocheld550d982006-06-27 00:41:40 -0400560 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/* bus */
564
565static int
Len Brown4be44fc2005-08-05 00:44:28 -0400566acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Len Brown4be44fc2005-08-05 00:44:28 -0400568 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400569 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400570 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
571 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 arg0.integer.value = option;
575
Patrick Mochel90130262006-05-19 16:54:48 -0400576 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400578 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Patrick Mocheld550d982006-06-27 00:41:40 -0400580 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
583static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400584acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 int status;
587
Patrick Mochel90130262006-05-19 16:54:48 -0400588 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Patrick Mocheld550d982006-06-27 00:41:40 -0400590 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
593static int
Len Brown4be44fc2005-08-05 00:44:28 -0400594acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400595 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Len Brown4be44fc2005-08-05 00:44:28 -0400597 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Patrick Mochel90130262006-05-19 16:54:48 -0400599 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 *options &= 3;
601
Patrick Mocheld550d982006-06-27 00:41:40 -0400602 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605/*
606 * Arg:
607 * video : video bus device pointer
608 * bios_flag :
609 * 0. The system BIOS should NOT automatically switch(toggle)
610 * the active display output.
611 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100612 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 * 2. The _DGS value should be locked.
614 * 3. The system BIOS should not automatically switch (toggle) the
615 * active display output, but instead generate the display switch
616 * event notify code.
617 * lcd_flag :
618 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100619 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100621 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 * Return Value:
623 * -1 wrong arg.
624 */
625
626static int
Len Brown4be44fc2005-08-05 00:44:28 -0400627acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Len Brown4be44fc2005-08-05 00:44:28 -0400629 acpi_integer status = 0;
630 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
631 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Len Brown4be44fc2005-08-05 00:44:28 -0400634 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 status = -1;
636 goto Failed;
637 }
638 arg0.integer.value = (lcd_flag << 2) | bios_flag;
639 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400640 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Len Brown4be44fc2005-08-05 00:44:28 -0400642 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400643 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
646/*
Zhang Rui935e5f22008-12-11 16:24:52 -0500647 * Simple comparison function used to sort backlight levels.
648 */
649
650static int
651acpi_video_cmp_level(const void *a, const void *b)
652{
653 return *(int *)a - *(int *)b;
654}
655
656/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * Arg:
658 * device : video output device (LCD, CRT, ..)
659 *
660 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100661 * Maximum brightness level
662 *
663 * Allocate and initialize device->brightness.
664 */
665
666static int
667acpi_video_init_brightness(struct acpi_video_device *device)
668{
669 union acpi_object *obj = NULL;
670 int i, max_level = 0, count = 0;
671 union acpi_object *o;
672 struct acpi_video_device_brightness *br = NULL;
673
674 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
675 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
676 "LCD brightness level\n"));
677 goto out;
678 }
679
680 if (obj->package.count < 2)
681 goto out;
682
683 br = kzalloc(sizeof(*br), GFP_KERNEL);
684 if (!br) {
685 printk(KERN_ERR "can't allocate memory\n");
686 goto out;
687 }
688
689 br->levels = kmalloc(obj->package.count * sizeof *(br->levels),
690 GFP_KERNEL);
691 if (!br->levels)
692 goto out_free;
693
694 for (i = 0; i < obj->package.count; i++) {
695 o = (union acpi_object *)&obj->package.elements[i];
696 if (o->type != ACPI_TYPE_INTEGER) {
697 printk(KERN_ERR PREFIX "Invalid data\n");
698 continue;
699 }
700 br->levels[count] = (u32) o->integer.value;
701
702 if (br->levels[count] > max_level)
703 max_level = br->levels[count];
704 count++;
705 }
706
Zhang Rui935e5f22008-12-11 16:24:52 -0500707 /* don't sort the first two brightness levels */
708 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
709 acpi_video_cmp_level, NULL);
710
Julia Jomantaite469778c2008-06-23 22:50:42 +0100711 if (count < 2)
712 goto out_free_levels;
713
714 br->count = count;
715 device->brightness = br;
716 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "found %d brightness levels\n", count));
717 kfree(obj);
718 return max_level;
719
720out_free_levels:
721 kfree(br->levels);
722out_free:
723 kfree(br);
724out:
725 device->brightness = NULL;
726 kfree(obj);
727 return 0;
728}
729
730/*
731 * Arg:
732 * device : video output device (LCD, CRT, ..)
733 *
734 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * None
736 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100737 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 * device.
739 */
740
Len Brown4be44fc2005-08-05 00:44:28 -0400741static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 acpi_handle h_dummy1;
Yu Luming2f3d0002006-11-11 02:40:34 +0800744 u32 max_level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
William Lee Irwin III98934de2007-12-12 03:56:55 -0800747 memset(&device->cap, 0, sizeof(device->cap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Patrick Mochel90130262006-05-19 16:54:48 -0400749 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 device->cap._ADR = 1;
751 }
Patrick Mochel90130262006-05-19 16:54:48 -0400752 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400753 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Patrick Mochel90130262006-05-19 16:54:48 -0400755 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400756 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800758 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
759 device->cap._BQC = 1;
Patrick Mochel90130262006-05-19 16:54:48 -0400760 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400761 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Patrick Mochel90130262006-05-19 16:54:48 -0400763 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 device->cap._DCS = 1;
765 }
Patrick Mochel90130262006-05-19 16:54:48 -0400766 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 device->cap._DGS = 1;
768 }
Patrick Mochel90130262006-05-19 16:54:48 -0400769 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 device->cap._DSS = 1;
771 }
772
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200773 if (acpi_video_backlight_support())
774 max_level = acpi_video_init_brightness(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Zhao Yakuic2c78902008-07-17 10:46:05 +0800776 if (device->cap._BCL && device->cap._BCM && max_level > 0) {
Zhang Rui702ed512008-01-17 15:51:22 +0800777 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800778 static int count = 0;
779 char *name;
Yu Luming2f3d0002006-11-11 02:40:34 +0800780 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
781 if (!name)
782 return;
783
Yu Luming2f3d0002006-11-11 02:40:34 +0800784 sprintf(name, "acpi_video%d", count++);
Yu Luming2f3d0002006-11-11 02:40:34 +0800785 device->backlight = backlight_device_register(name,
Richard Purdie599a52d2007-02-10 23:07:48 +0000786 NULL, device, &acpi_backlight_ops);
Matthew Garrett38531e62007-12-26 02:03:26 +0000787 device->backlight->props.max_brightness = device->brightness->count-3;
Zhao Yakuic2c78902008-07-17 10:46:05 +0800788 /*
789 * If there exists the _BQC object, the _BQC object will be
790 * called to get the current backlight brightness. Otherwise
791 * the brightness will be set to the maximum.
792 */
793 if (device->cap._BQC)
794 device->backlight->props.brightness =
795 acpi_video_get_brightness(device->backlight);
796 else
797 device->backlight->props.brightness =
798 device->backlight->props.max_brightness;
Richard Purdie599a52d2007-02-10 23:07:48 +0000799 backlight_update_status(device->backlight);
Yu Luming2f3d0002006-11-11 02:40:34 +0800800 kfree(name);
Zhang Rui702ed512008-01-17 15:51:22 +0800801
802 device->cdev = thermal_cooling_device_register("LCD",
803 device->dev, &video_cooling_ops);
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500804 if (IS_ERR(device->cdev))
805 return;
806
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200807 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
808 device->cdev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800809 result = sysfs_create_link(&device->dev->dev.kobj,
810 &device->cdev->device.kobj,
811 "thermal_cooling");
812 if (result)
813 printk(KERN_ERR PREFIX "Create sysfs link\n");
814 result = sysfs_create_link(&device->cdev->device.kobj,
815 &device->dev->dev.kobj, "device");
816 if (result)
817 printk(KERN_ERR PREFIX "Create sysfs link\n");
818
Yu Luming2f3d0002006-11-11 02:40:34 +0800819 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200820
821 if (acpi_video_display_switch_support()) {
822
823 if (device->cap._DCS && device->cap._DSS) {
824 static int count;
825 char *name;
826 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
827 if (!name)
828 return;
829 sprintf(name, "acpi_video%d", count++);
830 device->output_dev = video_output_register(name,
831 NULL, device, &acpi_output_properties);
832 kfree(name);
833 }
Luming Yu23b0f012007-05-09 21:07:05 +0800834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
837/*
838 * Arg:
839 * device : video output device (VGA)
840 *
841 * Return Value:
842 * None
843 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100844 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 */
846
Len Brown4be44fc2005-08-05 00:44:28 -0400847static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Len Brown4be44fc2005-08-05 00:44:28 -0400849 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
William Lee Irwin III98934de2007-12-12 03:56:55 -0800851 memset(&video->cap, 0, sizeof(video->cap));
Patrick Mochel90130262006-05-19 16:54:48 -0400852 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 video->cap._DOS = 1;
854 }
Patrick Mochel90130262006-05-19 16:54:48 -0400855 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 video->cap._DOD = 1;
857 }
Patrick Mochel90130262006-05-19 16:54:48 -0400858 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 video->cap._ROM = 1;
860 }
Patrick Mochel90130262006-05-19 16:54:48 -0400861 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 video->cap._GPD = 1;
863 }
Patrick Mochel90130262006-05-19 16:54:48 -0400864 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 video->cap._SPD = 1;
866 }
Patrick Mochel90130262006-05-19 16:54:48 -0400867 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 video->cap._VPO = 1;
869 }
870}
871
872/*
873 * Check whether the video bus device has required AML method to
874 * support the desired features
875 */
876
Len Brown4be44fc2005-08-05 00:44:28 -0400877static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Len Brown4be44fc2005-08-05 00:44:28 -0400879 acpi_status status = -ENOENT;
Thomas Renninger22c13f92008-08-01 17:37:54 +0200880 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400883 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Thomas Renninger22c13f92008-08-01 17:37:54 +0200885 dev = acpi_get_physical_pci_device(video->device->handle);
886 if (!dev)
887 return -ENODEV;
888 put_device(dev);
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /* Since there is no HID, CID and so on for VGA driver, we have
891 * to check well known required nodes.
892 */
893
Julius Volz98fb8fe2007-02-20 16:38:40 +0100894 /* Does this device support video switching? */
Len Brown4be44fc2005-08-05 00:44:28 -0400895 if (video->cap._DOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 video->flags.multihead = 1;
897 status = 0;
898 }
899
Julius Volz98fb8fe2007-02-20 16:38:40 +0100900 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -0400901 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 video->flags.rom = 1;
903 status = 0;
904 }
905
Julius Volz98fb8fe2007-02-20 16:38:40 +0100906 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -0400907 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 video->flags.post = 1;
909 status = 0;
910 }
911
Patrick Mocheld550d982006-06-27 00:41:40 -0400912 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915/* --------------------------------------------------------------------------
916 FS Interface (/proc)
917 -------------------------------------------------------------------------- */
918
Len Brown4be44fc2005-08-05 00:44:28 -0400919static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921/* video devices */
922
Len Brown4be44fc2005-08-05 00:44:28 -0400923static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200925 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (!dev)
929 goto end;
930
931 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
932 seq_printf(seq, "type: ");
933 if (dev->flags.crt)
934 seq_printf(seq, "CRT\n");
935 else if (dev->flags.lcd)
936 seq_printf(seq, "LCD\n");
937 else if (dev->flags.tvout)
938 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -0500939 else if (dev->flags.dvi)
940 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 else
942 seq_printf(seq, "UNKNOWN\n");
943
Len Brown4be44fc2005-08-05 00:44:28 -0400944 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Len Brown4be44fc2005-08-05 00:44:28 -0400946 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
950static int
Len Brown4be44fc2005-08-05 00:44:28 -0400951acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 return single_open(file, acpi_video_device_info_seq_show,
954 PDE(inode)->data);
955}
956
Len Brown4be44fc2005-08-05 00:44:28 -0400957static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Len Brown4be44fc2005-08-05 00:44:28 -0400959 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200960 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400961 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 if (!dev)
965 goto end;
966
967 status = acpi_video_device_get_state(dev, &state);
968 seq_printf(seq, "state: ");
969 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -0400970 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 else
972 seq_printf(seq, "<not supported>\n");
973
974 status = acpi_video_device_query(dev, &state);
975 seq_printf(seq, "query: ");
976 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -0400977 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 else
979 seq_printf(seq, "<not supported>\n");
980
Len Brown4be44fc2005-08-05 00:44:28 -0400981 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400982 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
985static int
Len Brown4be44fc2005-08-05 00:44:28 -0400986acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
988 return single_open(file, acpi_video_device_state_seq_show,
989 PDE(inode)->data);
990}
991
992static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400993acpi_video_device_write_state(struct file *file,
994 const char __user * buffer,
995 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Len Brown4be44fc2005-08-05 00:44:28 -0400997 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200998 struct seq_file *m = file->private_data;
999 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001000 char str[12] = { 0 };
1001 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 if (!dev || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001005 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001008 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 str[count] = 0;
1011 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001012 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 status = acpi_video_device_set_state(dev, state);
1015
1016 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -04001017 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Patrick Mocheld550d982006-06-27 00:41:40 -04001019 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022static int
Len Brown4be44fc2005-08-05 00:44:28 -04001023acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001025 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001026 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 if (!dev || !dev->brightness) {
1030 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001031 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033
1034 seq_printf(seq, "levels: ");
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001035 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 seq_printf(seq, " %d", dev->brightness->levels[i]);
1037 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1038
Patrick Mocheld550d982006-06-27 00:41:40 -04001039 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042static int
Len Brown4be44fc2005-08-05 00:44:28 -04001043acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 return single_open(file, acpi_video_device_brightness_seq_show,
1046 PDE(inode)->data);
1047}
1048
1049static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001050acpi_video_device_write_brightness(struct file *file,
1051 const char __user * buffer,
1052 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001054 struct seq_file *m = file->private_data;
1055 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001056 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001057 unsigned int level = 0;
1058 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Thomas Renninger59d399d2005-11-08 05:27:00 -05001061 if (!dev || !dev->brightness || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001062 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001065 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 str[count] = 0;
1068 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001071 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Julius Volz98fb8fe2007-02-20 16:38:40 +01001073 /* validate through the list of available levels */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001074 for (i = 2; i < dev->brightness->count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (level == dev->brightness->levels[i]) {
Len Brown4be44fc2005-08-05 00:44:28 -04001076 if (ACPI_SUCCESS
1077 (acpi_video_device_lcd_set_level(dev, level)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 dev->brightness->curr = level;
1079 break;
1080 }
1081
Patrick Mocheld550d982006-06-27 00:41:40 -04001082 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Len Brown4be44fc2005-08-05 00:44:28 -04001085static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001087 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001088 int status;
1089 int i;
1090 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if (!dev)
1094 goto out;
1095
Len Brown4be44fc2005-08-05 00:44:28 -04001096 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001098 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100
1101 if (ACPI_FAILURE(status)) {
1102 goto out;
1103 }
1104
1105 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1106 for (i = 0; i < edid->buffer.length; i++)
1107 seq_putc(seq, edid->buffer.pointer[i]);
1108 }
1109
Len Brown4be44fc2005-08-05 00:44:28 -04001110 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (!edid)
1112 seq_printf(seq, "<not supported>\n");
1113 else
1114 kfree(edid);
1115
Patrick Mocheld550d982006-06-27 00:41:40 -04001116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
1118
1119static int
Len Brown4be44fc2005-08-05 00:44:28 -04001120acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 return single_open(file, acpi_video_device_EDID_seq_show,
1123 PDE(inode)->data);
1124}
1125
Len Brown4be44fc2005-08-05 00:44:28 -04001126static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001128 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 struct acpi_video_device *vid_dev;
1130
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001131 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001133 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001135 device_dir = proc_mkdir(acpi_device_bid(device),
1136 vid_dev->video->dir);
1137 if (!device_dir)
1138 return -ENOMEM;
1139
1140 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001143 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001144 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001146 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001149 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001150 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001151 &acpi_video_device_state_fops,
1152 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001154 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001157 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001158 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001159 &acpi_video_device_brightness_fops,
1160 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001162 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001165 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001166 &acpi_video_device_EDID_fops,
1167 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001169 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001171 acpi_device_dir(device) = device_dir;
1172
Patrick Mocheld550d982006-06-27 00:41:40 -04001173 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001174
1175 err_remove_brightness:
1176 remove_proc_entry("brightness", device_dir);
1177 err_remove_state:
1178 remove_proc_entry("state", device_dir);
1179 err_remove_info:
1180 remove_proc_entry("info", device_dir);
1181 err_remove_dir:
1182 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1183 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
Len Brown4be44fc2005-08-05 00:44:28 -04001186static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001189 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001191 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001193 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001195 device_dir = acpi_device_dir(device);
1196 if (device_dir) {
1197 remove_proc_entry("info", device_dir);
1198 remove_proc_entry("state", device_dir);
1199 remove_proc_entry("brightness", device_dir);
1200 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001201 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 acpi_device_dir(device) = NULL;
1203 }
1204
Patrick Mocheld550d982006-06-27 00:41:40 -04001205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206}
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001209static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001211 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 if (!video)
1215 goto end;
1216
1217 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001218 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001220 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001222 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Len Brown4be44fc2005-08-05 00:44:28 -04001224 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Len Brown4be44fc2005-08-05 00:44:28 -04001228static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Len Brown4be44fc2005-08-05 00:44:28 -04001230 return single_open(file, acpi_video_bus_info_seq_show,
1231 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Len Brown4be44fc2005-08-05 00:44:28 -04001234static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001236 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 if (!video)
1240 goto end;
1241
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001242 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 seq_printf(seq, "<TODO>\n");
1244
Len Brown4be44fc2005-08-05 00:44:28 -04001245 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
Len Brown4be44fc2005-08-05 00:44:28 -04001249static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1252}
1253
Len Brown4be44fc2005-08-05 00:44:28 -04001254static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001256 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001257 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001258 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 if (!video)
1262 goto end;
1263
1264 status = acpi_video_bus_POST_options(video, &options);
1265 if (ACPI_SUCCESS(status)) {
1266 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001267 printk(KERN_WARNING PREFIX
1268 "The motherboard VGA device is not listed as a possible POST device.\n");
1269 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001270 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Frank Seidel4d939152009-02-04 17:03:07 +01001272 printk(KERN_WARNING "%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001273 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (options & 2)
1275 seq_printf(seq, " <PCI video>");
1276 if (options & 4)
1277 seq_printf(seq, " <AGP video>");
1278 seq_putc(seq, '\n');
1279 } else
1280 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001281 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
1285static int
Len Brown4be44fc2005-08-05 00:44:28 -04001286acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Len Brown4be44fc2005-08-05 00:44:28 -04001288 return single_open(file, acpi_video_bus_POST_info_seq_show,
1289 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Len Brown4be44fc2005-08-05 00:44:28 -04001292static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001294 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001295 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001296 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 if (!video)
1300 goto end;
1301
Len Brown4be44fc2005-08-05 00:44:28 -04001302 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (!ACPI_SUCCESS(status)) {
1304 seq_printf(seq, "<not supported>\n");
1305 goto end;
1306 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001307 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Len Brown4be44fc2005-08-05 00:44:28 -04001309 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001310 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
Len Brown4be44fc2005-08-05 00:44:28 -04001313static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001315 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Len Brown4be44fc2005-08-05 00:44:28 -04001318 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Patrick Mocheld550d982006-06-27 00:41:40 -04001320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
Len Brown4be44fc2005-08-05 00:44:28 -04001323static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Len Brown4be44fc2005-08-05 00:44:28 -04001325 return single_open(file, acpi_video_bus_POST_seq_show,
1326 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Len Brown4be44fc2005-08-05 00:44:28 -04001329static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1332}
1333
1334static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001335acpi_video_bus_write_POST(struct file *file,
1336 const char __user * buffer,
1337 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Len Brown4be44fc2005-08-05 00:44:28 -04001339 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001340 struct seq_file *m = file->private_data;
1341 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001342 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001343 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001347 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 status = acpi_video_bus_POST_options(video, &options);
1350 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001351 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001354 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 str[count] = 0;
1357 opt = strtoul(str, NULL, 0);
1358 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001359 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Julius Volz98fb8fe2007-02-20 16:38:40 +01001361 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 options |= 1;
1363
1364 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001365 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001367 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 }
1370
Patrick Mocheld550d982006-06-27 00:41:40 -04001371 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
1374static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001375acpi_video_bus_write_DOS(struct file *file,
1376 const char __user * buffer,
1377 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Len Brown4be44fc2005-08-05 00:44:28 -04001379 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001380 struct seq_file *m = file->private_data;
1381 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001382 char str[12] = { 0 };
1383 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001387 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001390 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 str[count] = 0;
1393 opt = strtoul(str, NULL, 0);
1394 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001395 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Len Brown4be44fc2005-08-05 00:44:28 -04001397 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001400 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Patrick Mocheld550d982006-06-27 00:41:40 -04001402 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
Len Brown4be44fc2005-08-05 00:44:28 -04001405static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001407 struct acpi_video_bus *video = acpi_driver_data(device);
1408 struct proc_dir_entry *device_dir;
1409 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001411 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1412 if (!device_dir)
1413 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001415 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001418 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001419 &acpi_video_bus_info_fops,
1420 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001422 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001425 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001426 &acpi_video_bus_ROM_fops,
1427 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001429 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001432 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001433 &acpi_video_bus_POST_info_fops,
1434 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001436 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 /* 'POST' [R/W] */
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001439 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001440 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001441 &acpi_video_bus_POST_fops,
1442 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001444 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 /* 'DOS' [R/W] */
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001447 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001448 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001449 &acpi_video_bus_DOS_fops,
1450 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001452 goto err_remove_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001454 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001455 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001456
1457 err_remove_post:
1458 remove_proc_entry("POST", device_dir);
1459 err_remove_post_info:
1460 remove_proc_entry("POST_info", device_dir);
1461 err_remove_rom:
1462 remove_proc_entry("ROM", device_dir);
1463 err_remove_info:
1464 remove_proc_entry("info", device_dir);
1465 err_remove_dir:
1466 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1467 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Len Brown4be44fc2005-08-05 00:44:28 -04001470static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001472 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001474 if (device_dir) {
1475 remove_proc_entry("info", device_dir);
1476 remove_proc_entry("ROM", device_dir);
1477 remove_proc_entry("POST_info", device_dir);
1478 remove_proc_entry("POST", device_dir);
1479 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001480 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 acpi_device_dir(device) = NULL;
1482 }
1483
Patrick Mocheld550d982006-06-27 00:41:40 -04001484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
1487/* --------------------------------------------------------------------------
1488 Driver Interface
1489 -------------------------------------------------------------------------- */
1490
1491/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001492static struct acpi_video_device_attrib*
1493acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1494{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001495 struct acpi_video_enumerated_device *ids;
1496 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001497
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001498 for (i = 0; i < video->attached_count; i++) {
1499 ids = &video->attached_array[i];
1500 if ((ids->value.int_val & 0xffff) == device_id)
1501 return &ids->value.attrib;
1502 }
1503
Rui Zhang82cae992007-01-03 23:40:53 -05001504 return NULL;
1505}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507static int
Len Brown4be44fc2005-08-05 00:44:28 -04001508acpi_video_bus_get_one_device(struct acpi_device *device,
1509 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001511 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001512 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001513 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001514 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001517 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Len Brown4be44fc2005-08-05 00:44:28 -04001519 status =
1520 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (ACPI_SUCCESS(status)) {
1522
Burman Yan36bcbec2006-12-19 12:56:11 -08001523 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001525 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1528 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001529 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 data->device_id = device_id;
1532 data->video = video;
1533 data->dev = device;
1534
Rui Zhang82cae992007-01-03 23:40:53 -05001535 attribute = acpi_video_get_device_attr(video, device_id);
1536
1537 if((attribute != NULL) && attribute->device_id_scheme) {
1538 switch (attribute->display_type) {
1539 case ACPI_VIDEO_DISPLAY_CRT:
1540 data->flags.crt = 1;
1541 break;
1542 case ACPI_VIDEO_DISPLAY_TV:
1543 data->flags.tvout = 1;
1544 break;
1545 case ACPI_VIDEO_DISPLAY_DVI:
1546 data->flags.dvi = 1;
1547 break;
1548 case ACPI_VIDEO_DISPLAY_LCD:
1549 data->flags.lcd = 1;
1550 break;
1551 default:
1552 data->flags.unknown = 1;
1553 break;
1554 }
1555 if(attribute->bios_can_detect)
1556 data->flags.bios = 1;
1557 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 acpi_video_device_bind(video, data);
1561 acpi_video_device_find_cap(data);
1562
Patrick Mochel90130262006-05-19 16:54:48 -04001563 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001564 ACPI_DEVICE_NOTIFY,
1565 acpi_video_device_notify,
1566 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001568 printk(KERN_ERR PREFIX
1569 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001570 if(data->brightness)
1571 kfree(data->brightness->levels);
1572 kfree(data->brightness);
1573 kfree(data);
1574 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
1576
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001577 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001579 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 acpi_video_device_add_fs(device);
1582
Patrick Mocheld550d982006-06-27 00:41:40 -04001583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585
Patrick Mocheld550d982006-06-27 00:41:40 -04001586 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
1589/*
1590 * Arg:
1591 * video : video bus device
1592 *
1593 * Return:
1594 * none
1595 *
1596 * Enumerate the video device list of the video bus,
1597 * bind the ids with the corresponding video devices
1598 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001599 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Len Brown4be44fc2005-08-05 00:44:28 -04001601static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001603 struct acpi_video_device *dev;
1604
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001605 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001606
1607 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001608 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001609
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001610 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
1613/*
1614 * Arg:
1615 * video : video bus device
1616 * device : video output device under the video
1617 * bus
1618 *
1619 * Return:
1620 * none
1621 *
1622 * Bind the ids with the corresponding video devices
1623 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001624 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626static void
Len Brown4be44fc2005-08-05 00:44:28 -04001627acpi_video_device_bind(struct acpi_video_bus *video,
1628 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001630 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001631 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001633 for (i = 0; i < video->attached_count; i++) {
1634 ids = &video->attached_array[i];
1635 if (device->device_id == (ids->value.int_val & 0xffff)) {
1636 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1638 }
1639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641
1642/*
1643 * Arg:
1644 * video : video bus device
1645 *
1646 * Return:
1647 * < 0 : error
1648 *
1649 * Call _DOD to enumerate all devices attached to display adapter
1650 *
Len Brown4be44fc2005-08-05 00:44:28 -04001651 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1654{
Len Brown4be44fc2005-08-05 00:44:28 -04001655 int status;
1656 int count;
1657 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001658 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001659 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1660 union acpi_object *dod = NULL;
1661 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Patrick Mochel90130262006-05-19 16:54:48 -04001663 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001665 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001666 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001669 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001671 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 status = -EFAULT;
1673 goto out;
1674 }
1675
1676 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001677 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001679 active_list = kcalloc(1 + dod->package.count,
1680 sizeof(struct acpi_video_enumerated_device),
1681 GFP_KERNEL);
1682 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 status = -ENOMEM;
1684 goto out;
1685 }
1686
1687 count = 0;
1688 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001689 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001692 printk(KERN_ERR PREFIX
1693 "Invalid _DOD data in element %d\n", i);
1694 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001696
1697 active_list[count].value.int_val = obj->integer.value;
1698 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001699 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1700 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 count++;
1702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Jesper Juhl6044ec82005-11-07 01:01:32 -08001704 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001705
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001706 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001708
1709 out:
Len Brown02438d82006-06-30 03:19:10 -04001710 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001711 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
Len Brown4be44fc2005-08-05 00:44:28 -04001714static int
1715acpi_video_get_next_level(struct acpi_video_device *device,
1716 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001718 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001719 max = max_below = 0;
1720 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001721 /* Find closest level to level_current */
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001722 for (i = 2; i < device->brightness->count; i++) {
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001723 l = device->brightness->levels[i];
1724 if (abs(l - level_current) < abs(delta)) {
1725 delta = l - level_current;
1726 if (!delta)
1727 break;
1728 }
1729 }
1730 /* Ajust level_current to closest available level */
1731 level_current += delta;
Zhao Yakui0a3db1c2009-02-02 11:33:41 +08001732 for (i = 2; i < device->brightness->count; i++) {
Thomas Tuttlef4715182006-12-19 12:56:14 -08001733 l = device->brightness->levels[i];
1734 if (l < min)
1735 min = l;
1736 if (l > max)
1737 max = l;
1738 if (l < min_above && l > level_current)
1739 min_above = l;
1740 if (l > max_below && l < level_current)
1741 max_below = l;
1742 }
1743
1744 switch (event) {
1745 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1746 return (level_current < max) ? min_above : min;
1747 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1748 return (level_current < max) ? min_above : max;
1749 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1750 return (level_current > min) ? max_below : min;
1751 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1752 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1753 return 0;
1754 default:
1755 return level_current;
1756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757}
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759static void
Len Brown4be44fc2005-08-05 00:44:28 -04001760acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001762 unsigned long long level_current, level_next;
Julia Jomantaite469778c2008-06-23 22:50:42 +01001763 if (!device->brightness)
1764 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 acpi_video_device_lcd_get_level_current(device, &level_current);
1766 level_next = acpi_video_get_next_level(device, level_current, event);
1767 acpi_video_device_lcd_set_level(device, level_next);
1768}
1769
1770static int
Len Brown4be44fc2005-08-05 00:44:28 -04001771acpi_video_bus_get_devices(struct acpi_video_bus *video,
1772 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Len Brown4be44fc2005-08-05 00:44:28 -04001774 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001775 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 acpi_video_device_enumerate(video);
1778
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001779 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 status = acpi_video_bus_get_one_device(dev, video);
1782 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001783 printk(KERN_WARNING PREFIX
1784 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 continue;
1786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001788 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
Len Brown4be44fc2005-08-05 00:44:28 -04001791static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Karol Kozimor031ec772005-07-30 04:18:00 -04001793 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 struct acpi_video_bus *video;
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001798 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 video = device->video;
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 acpi_video_device_remove_fs(device->dev);
1803
Patrick Mochel90130262006-05-19 16:54:48 -04001804 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001805 ACPI_DEVICE_NOTIFY,
1806 acpi_video_device_notify);
Richard Purdie599a52d2007-02-10 23:07:48 +00001807 backlight_device_unregister(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +08001808 if (device->cdev) {
1809 sysfs_remove_link(&device->dev->dev.kobj,
1810 "thermal_cooling");
1811 sysfs_remove_link(&device->cdev->device.kobj,
1812 "device");
1813 thermal_cooling_device_unregister(device->cdev);
1814 device->cdev = NULL;
1815 }
Luming Yu23b0f012007-05-09 21:07:05 +08001816 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001817
Patrick Mocheld550d982006-06-27 00:41:40 -04001818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820
Len Brown4be44fc2005-08-05 00:44:28 -04001821static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Len Brown4be44fc2005-08-05 00:44:28 -04001823 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001824 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001826 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001828 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001830 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04001831 if (ACPI_FAILURE(status))
1832 printk(KERN_WARNING PREFIX
1833 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001835 if (dev->brightness) {
1836 kfree(dev->brightness->levels);
1837 kfree(dev->brightness);
1838 }
1839 list_del(&dev->entry);
1840 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001843 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001844
Patrick Mocheld550d982006-06-27 00:41:40 -04001845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
1848/* acpi_video interface */
1849
Len Brown4be44fc2005-08-05 00:44:28 -04001850static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Zhang Ruia21101c2007-09-14 11:46:22 +08001852 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
Len Brown4be44fc2005-08-05 00:44:28 -04001855static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 return acpi_video_bus_DOS(video, 0, 1);
1858}
1859
Len Brown4be44fc2005-08-05 00:44:28 -04001860static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001862 struct acpi_video_bus *video = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001863 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001864 struct input_dev *input;
1865 int keycode;
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001868 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001870 device = video->device;
Luming Yue9dab192007-08-20 18:23:53 +08001871 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
1873 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001874 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 * most likely via hotkey. */
Len Brown14e04fb32007-08-23 15:20:26 -04001876 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001877 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 break;
1879
Julius Volz98fb8fe2007-02-20 16:38:40 +01001880 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 * connector. */
1882 acpi_video_device_enumerate(video);
1883 acpi_video_device_rebind(video);
Len Brown14e04fb32007-08-23 15:20:26 -04001884 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001885 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 break;
1887
Len Brown4be44fc2005-08-05 00:44:28 -04001888 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001889 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001890 keycode = KEY_SWITCHVIDEOMODE;
1891 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001892 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001893 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001894 keycode = KEY_VIDEO_NEXT;
1895 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001896 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb32007-08-23 15:20:26 -04001897 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001898 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 break;
1900
1901 default:
Luming Yue9dab192007-08-20 18:23:53 +08001902 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001904 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 break;
1906 }
1907
Zhang Rui7761f632008-01-25 14:48:12 +08001908 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001909 input_report_key(input, keycode, 1);
1910 input_sync(input);
1911 input_report_key(input, keycode, 0);
1912 input_sync(input);
1913
Patrick Mocheld550d982006-06-27 00:41:40 -04001914 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
Len Brown4be44fc2005-08-05 00:44:28 -04001917static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001919 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001920 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001921 struct acpi_video_bus *bus;
1922 struct input_dev *input;
1923 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001926 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001928 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001929 bus = video_device->video;
1930 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001933 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001934 if (brightness_switch_enabled)
1935 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001936 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001937 keycode = KEY_BRIGHTNESS_CYCLE;
1938 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001939 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001940 if (brightness_switch_enabled)
1941 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001942 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001943 keycode = KEY_BRIGHTNESSUP;
1944 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001945 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001946 if (brightness_switch_enabled)
1947 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001948 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001949 keycode = KEY_BRIGHTNESSDOWN;
1950 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001951 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08001952 if (brightness_switch_enabled)
1953 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001954 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001955 keycode = KEY_BRIGHTNESS_ZERO;
1956 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001957 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08001958 if (brightness_switch_enabled)
1959 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb32007-08-23 15:20:26 -04001960 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001961 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 break;
1963 default:
Luming Yue9dab192007-08-20 18:23:53 +08001964 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001966 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 break;
1968 }
Luming Yue9dab192007-08-20 18:23:53 +08001969
Zhang Rui7761f632008-01-25 14:48:12 +08001970 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001971 input_report_key(input, keycode, 1);
1972 input_sync(input);
1973 input_report_key(input, keycode, 0);
1974 input_sync(input);
1975
Patrick Mocheld550d982006-06-27 00:41:40 -04001976 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
Zhang Ruie6d9da12007-08-25 02:23:31 -04001979static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08001980static int acpi_video_resume(struct acpi_device *device)
1981{
1982 struct acpi_video_bus *video;
1983 struct acpi_video_device *video_device;
1984 int i;
1985
1986 if (!device || !acpi_driver_data(device))
1987 return -EINVAL;
1988
1989 video = acpi_driver_data(device);
1990
1991 for (i = 0; i < video->attached_count; i++) {
1992 video_device = video->attached_array[i].bind_info;
1993 if (video_device && video_device->backlight)
1994 acpi_video_set_brightness(video_device->backlight);
1995 }
1996 return AE_OK;
1997}
1998
Len Brown4be44fc2005-08-05 00:44:28 -04001999static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002001 acpi_status status;
2002 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08002003 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002004 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Burman Yan36bcbec2006-12-19 12:56:11 -08002006 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04002008 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Zhang Ruie6d9da12007-08-25 02:23:31 -04002010 /* a hack to fix the duplicate name "VID" problem on T61 */
2011 if (!strcmp(device->pnp.bus_id, "VID")) {
2012 if (instance)
2013 device->pnp.bus_id[3] = '0' + instance;
2014 instance ++;
2015 }
Zhao Yakuif3b39f12009-02-02 22:55:01 -05002016 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2017 if (!strcmp(device->pnp.bus_id, "VGA")) {
2018 if (instance)
2019 device->pnp.bus_id[3] = '0' + instance;
2020 instance++;
2021 }
Zhang Ruie6d9da12007-08-25 02:23:31 -04002022
Patrick Mochele6afa0d2006-05-19 16:54:40 -04002023 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2025 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002026 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002029 error = acpi_video_bus_check(video);
2030 if (error)
2031 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002033 error = acpi_video_bus_add_fs(device);
2034 if (error)
2035 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002037 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 INIT_LIST_HEAD(&video->video_device_list);
2039
2040 acpi_video_bus_get_devices(video, device);
2041 acpi_video_bus_start_devices(video);
2042
Patrick Mochel90130262006-05-19 16:54:48 -04002043 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002044 ACPI_DEVICE_NOTIFY,
2045 acpi_video_bus_notify, video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002047 printk(KERN_ERR PREFIX
2048 "Error installing notify handler\n");
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002049 error = -ENODEV;
2050 goto err_stop_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052
Luming Yue9dab192007-08-20 18:23:53 +08002053 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002054 if (!input) {
2055 error = -ENOMEM;
2056 goto err_uninstall_notify;
2057 }
Luming Yue9dab192007-08-20 18:23:53 +08002058
2059 snprintf(video->phys, sizeof(video->phys),
2060 "%s/video/input0", acpi_device_hid(video->device));
2061
2062 input->name = acpi_device_name(video->device);
2063 input->phys = video->phys;
2064 input->id.bustype = BUS_HOST;
2065 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002066 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002067 input->evbit[0] = BIT(EV_KEY);
2068 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2069 set_bit(KEY_VIDEO_NEXT, input->keybit);
2070 set_bit(KEY_VIDEO_PREV, input->keybit);
2071 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2072 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2073 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2074 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2075 set_bit(KEY_DISPLAY_OFF, input->keybit);
2076 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002077
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002078 error = input_register_device(input);
2079 if (error)
2080 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002083 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2084 video->flags.multihead ? "yes" : "no",
2085 video->flags.rom ? "yes" : "no",
2086 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002088 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002090 err_free_input_dev:
2091 input_free_device(input);
2092 err_uninstall_notify:
2093 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
2094 acpi_video_bus_notify);
2095 err_stop_video:
2096 acpi_video_bus_stop_devices(video);
2097 acpi_video_bus_put_devices(video);
2098 kfree(video->attached_array);
2099 acpi_video_bus_remove_fs(device);
2100 err_free_video:
2101 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002102 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002103
2104 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Len Brown4be44fc2005-08-05 00:44:28 -04002107static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Len Brown4be44fc2005-08-05 00:44:28 -04002109 acpi_status status = 0;
2110 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002114 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002116 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 acpi_video_bus_stop_devices(video);
2119
Patrick Mochel90130262006-05-19 16:54:48 -04002120 status = acpi_remove_notify_handler(video->device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002121 ACPI_DEVICE_NOTIFY,
2122 acpi_video_bus_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 acpi_video_bus_put_devices(video);
2125 acpi_video_bus_remove_fs(device);
2126
Luming Yue9dab192007-08-20 18:23:53 +08002127 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002128 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 kfree(video);
2130
Patrick Mocheld550d982006-06-27 00:41:40 -04002131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
Len Brown4be44fc2005-08-05 00:44:28 -04002134static int __init acpi_video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Len Brown4be44fc2005-08-05 00:44:28 -04002136 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2139 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002140 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 acpi_video_dir->owner = THIS_MODULE;
2142
2143 result = acpi_bus_register_driver(&acpi_video_bus);
2144 if (result < 0) {
2145 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002146 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148
Patrick Mocheld550d982006-06-27 00:41:40 -04002149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Len Brown4be44fc2005-08-05 00:44:28 -04002152static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 acpi_bus_unregister_driver(&acpi_video_bus);
2156
2157 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2158
Patrick Mocheld550d982006-06-27 00:41:40 -04002159 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161
2162module_init(acpi_video_init);
2163module_exit(acpi_video_exit);