blob: 6597c2a37c360645bac11987db7074ea63c46686 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/uaccess.h>
40
41#include <acpi/acpi_bus.h>
42#include <acpi/acpi_drivers.h>
43
44#define ACPI_VIDEO_COMPONENT 0x08000000
45#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 Brownc2b6705b2007-02-12 23:33:40 -050088 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020090 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .ops = {
92 .add = acpi_video_bus_add,
93 .remove = acpi_video_bus_remove,
Matthew Garrett863c1492008-02-04 23:31:24 -080094 .resume = acpi_video_resume,
Len Brown4be44fc2005-08-05 00:44:28 -040095 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070096};
97
98struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -040099 u8 multihead:1; /* can switch video heads */
100 u8 rom:1; /* can retrieve a video rom */
101 u8 post:1; /* can configure the head to */
102 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105struct acpi_video_bus_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400106 u8 _DOS:1; /*Enable/Disable output switching */
107 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
108 u8 _ROM:1; /*Get ROM Data */
109 u8 _GPD:1; /*Get POST Device */
110 u8 _SPD:1; /*Set POST Device */
111 u8 _VPO:1; /*Video POST Options */
112 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
Len Brown4be44fc2005-08-05 00:44:28 -0400115struct acpi_video_device_attrib {
116 u32 display_index:4; /* A zero-based instance of the Display */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100117 u32 display_port_attachment:4; /*This field differentiates the display type */
Len Brown4be44fc2005-08-05 00:44:28 -0400118 u32 display_type:4; /*Describe the specific type in use */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100119 u32 vendor_specific:4; /*Chipset Vendor Specific */
Len Brown4be44fc2005-08-05 00:44:28 -0400120 u32 bios_can_detect:1; /*BIOS can detect the device */
121 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
122 the VGA device. */
123 u32 pipe_id:3; /*For VGA multiple-head devices. */
124 u32 reserved:10; /*Must be 0 */
125 u32 device_id_scheme:1; /*Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128struct acpi_video_enumerated_device {
129 union {
130 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400131 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 } value;
133 struct acpi_video_device *bind_info;
134};
135
136struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400137 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400138 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400140 u8 attached_count;
141 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400143 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500144 struct mutex device_list_lock; /* protects video_device_list */
Len Brown4be44fc2005-08-05 00:44:28 -0400145 struct proc_dir_entry *dir;
Luming Yue9dab192007-08-20 18:23:53 +0800146 struct input_dev *input;
147 char phys[32]; /* for input device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
150struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400151 u8 crt:1;
152 u8 lcd:1;
153 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500154 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400155 u8 bios:1;
156 u8 unknown:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500157 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
160struct acpi_video_device_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400161 u8 _ADR:1; /*Return the unique ID */
162 u8 _BCL:1; /*Query list of brightness control levels supported */
163 u8 _BCM:1; /*Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800164 u8 _BQC:1; /* Get current brightness level */
Len Brown4be44fc2005-08-05 00:44:28 -0400165 u8 _DDC:1; /*Return the EDID for this device */
166 u8 _DCS:1; /*Return status of output device */
167 u8 _DGS:1; /*Query graphics state */
168 u8 _DSS:1; /*Device state set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
170
171struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400172 int curr;
173 int count;
174 int *levels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
177struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400178 unsigned long device_id;
179 struct acpi_video_device_flags flags;
180 struct acpi_video_device_cap cap;
181 struct list_head entry;
182 struct acpi_video_bus *video;
183 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800185 struct backlight_device *backlight;
Zhang Rui702ed512008-01-17 15:51:22 +0800186 struct thermal_cooling_device *cdev;
Luming Yu23b0f012007-05-09 21:07:05 +0800187 struct output_device *output_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/* bus */
191static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
192static struct file_operations acpi_video_bus_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700193 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400194 .open = acpi_video_bus_info_open_fs,
195 .read = seq_read,
196 .llseek = seq_lseek,
197 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198};
199
200static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
201static struct file_operations acpi_video_bus_ROM_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700202 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400203 .open = acpi_video_bus_ROM_open_fs,
204 .read = seq_read,
205 .llseek = seq_lseek,
206 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207};
208
Len Brown4be44fc2005-08-05 00:44:28 -0400209static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
210 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static struct file_operations acpi_video_bus_POST_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700212 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400213 .open = acpi_video_bus_POST_info_open_fs,
214 .read = seq_read,
215 .llseek = seq_lseek,
216 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217};
218
219static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
220static struct file_operations acpi_video_bus_POST_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700221 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400222 .open = acpi_video_bus_POST_open_fs,
223 .read = seq_read,
224 .llseek = seq_lseek,
225 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
229static struct file_operations acpi_video_bus_DOS_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700230 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400231 .open = acpi_video_bus_DOS_open_fs,
232 .read = seq_read,
233 .llseek = seq_lseek,
234 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235};
236
237/* device */
Len Brown4be44fc2005-08-05 00:44:28 -0400238static int acpi_video_device_info_open_fs(struct inode *inode,
239 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static struct file_operations acpi_video_device_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700241 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400242 .open = acpi_video_device_info_open_fs,
243 .read = seq_read,
244 .llseek = seq_lseek,
245 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246};
247
Len Brown4be44fc2005-08-05 00:44:28 -0400248static int acpi_video_device_state_open_fs(struct inode *inode,
249 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250static struct file_operations acpi_video_device_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700251 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400252 .open = acpi_video_device_state_open_fs,
253 .read = seq_read,
254 .llseek = seq_lseek,
255 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
Len Brown4be44fc2005-08-05 00:44:28 -0400258static int acpi_video_device_brightness_open_fs(struct inode *inode,
259 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static struct file_operations acpi_video_device_brightness_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700261 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400262 .open = acpi_video_device_brightness_open_fs,
263 .read = seq_read,
264 .llseek = seq_lseek,
265 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266};
267
Len Brown4be44fc2005-08-05 00:44:28 -0400268static int acpi_video_device_EDID_open_fs(struct inode *inode,
269 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270static struct file_operations acpi_video_device_EDID_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700271 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400272 .open = acpi_video_device_EDID_open_fs,
273 .read = seq_read,
274 .llseek = seq_lseek,
275 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276};
277
Len Brown4be44fc2005-08-05 00:44:28 -0400278static char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 "motherboard VGA device",
280 "PCI VGA device",
281 "AGP VGA device",
282 "UNKNOWN",
283};
284
Len Brown4be44fc2005-08-05 00:44:28 -0400285static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
286static void acpi_video_device_rebind(struct acpi_video_bus *video);
287static void acpi_video_device_bind(struct acpi_video_bus *video,
288 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800290static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
291 int level);
292static int acpi_video_device_lcd_get_level_current(
293 struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400294 unsigned long long *level);
Len Brown4be44fc2005-08-05 00:44:28 -0400295static int acpi_video_get_next_level(struct acpi_video_device *device,
296 u32 level_current, u32 event);
297static void acpi_video_switch_brightness(struct acpi_video_device *device,
298 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800299static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400300 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800301static int acpi_video_output_get(struct output_device *od);
302static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Yu Luming2f3d0002006-11-11 02:40:34 +0800304/*backlight device sysfs support*/
305static int acpi_video_get_brightness(struct backlight_device *bd)
306{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400307 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000308 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800309 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100310 (struct acpi_video_device *)bl_get_data(bd);
Yu Luming2f3d0002006-11-11 02:40:34 +0800311 acpi_video_device_lcd_get_level_current(vd, &cur_level);
Matthew Garrett38531e62007-12-26 02:03:26 +0000312 for (i = 2; i < vd->brightness->count; i++) {
313 if (vd->brightness->levels[i] == cur_level)
314 /* The first two entries are special - see page 575
315 of the ACPI spec 3.0 */
316 return i-2;
317 }
318 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800319}
320
321static int acpi_video_set_brightness(struct backlight_device *bd)
322{
Matthew Garrett38531e62007-12-26 02:03:26 +0000323 int request_level = bd->props.brightness+2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800324 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100325 (struct acpi_video_device *)bl_get_data(bd);
Matthew Garrett38531e62007-12-26 02:03:26 +0000326 acpi_video_device_lcd_set_level(vd,
327 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800328 return 0;
329}
330
Richard Purdie599a52d2007-02-10 23:07:48 +0000331static struct backlight_ops acpi_backlight_ops = {
332 .get_brightness = acpi_video_get_brightness,
333 .update_status = acpi_video_set_brightness,
334};
335
Luming Yu23b0f012007-05-09 21:07:05 +0800336/*video output device sysfs support*/
337static int acpi_video_output_get(struct output_device *od)
338{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400339 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800340 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700341 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800342 acpi_video_device_get_state(vd, &state);
343 return (int)state;
344}
345
346static int acpi_video_output_set(struct output_device *od)
347{
348 unsigned long state = od->request_state;
349 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700350 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800351 return acpi_video_device_set_state(vd, state);
352}
353
354static struct output_properties acpi_output_properties = {
355 .set_state = acpi_video_output_set,
356 .get_status = acpi_video_output_get,
357};
Zhang Rui702ed512008-01-17 15:51:22 +0800358
359
360/* thermal cooling device callbacks */
361static int video_get_max_state(struct thermal_cooling_device *cdev, char *buf)
362{
363 struct acpi_device *device = cdev->devdata;
364 struct acpi_video_device *video = acpi_driver_data(device);
365
366 return sprintf(buf, "%d\n", video->brightness->count - 3);
367}
368
369static int video_get_cur_state(struct thermal_cooling_device *cdev, char *buf)
370{
371 struct acpi_device *device = cdev->devdata;
372 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400373 unsigned long long level;
Zhang Rui702ed512008-01-17 15:51:22 +0800374 int state;
375
376 acpi_video_device_lcd_get_level_current(video, &level);
377 for (state = 2; state < video->brightness->count; state++)
378 if (level == video->brightness->levels[state])
379 return sprintf(buf, "%d\n",
380 video->brightness->count - state - 1);
381
382 return -EINVAL;
383}
384
385static int
386video_set_cur_state(struct thermal_cooling_device *cdev, unsigned int state)
387{
388 struct acpi_device *device = cdev->devdata;
389 struct acpi_video_device *video = acpi_driver_data(device);
390 int level;
391
392 if ( state >= video->brightness->count - 2)
393 return -EINVAL;
394
395 state = video->brightness->count - state;
396 level = video->brightness->levels[state -1];
397 return acpi_video_device_lcd_set_level(video, level);
398}
399
400static struct thermal_cooling_device_ops video_cooling_ops = {
401 .get_max_state = video_get_max_state,
402 .get_cur_state = video_get_cur_state,
403 .set_cur_state = video_set_cur_state,
404};
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/* --------------------------------------------------------------------------
407 Video Management
408 -------------------------------------------------------------------------- */
409
410/* device */
411
412static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400413acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Len Brown4be44fc2005-08-05 00:44:28 -0400415 int status;
Patrick Mochel90130262006-05-19 16:54:48 -0400416
417 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Patrick Mocheld550d982006-06-27 00:41:40 -0400419 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422static int
Len Brown4be44fc2005-08-05 00:44:28 -0400423acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400424 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Len Brown4be44fc2005-08-05 00:44:28 -0400426 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Patrick Mochel90130262006-05-19 16:54:48 -0400428 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Patrick Mocheld550d982006-06-27 00:41:40 -0400430 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
433static int
Len Brown4be44fc2005-08-05 00:44:28 -0400434acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Len Brown4be44fc2005-08-05 00:44:28 -0400436 int status;
437 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
438 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400439 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400443 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Patrick Mocheld550d982006-06-27 00:41:40 -0400445 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448static int
Len Brown4be44fc2005-08-05 00:44:28 -0400449acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
450 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Len Brown4be44fc2005-08-05 00:44:28 -0400452 int status;
453 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
454 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 *levels = NULL;
458
Patrick Mochel90130262006-05-19 16:54:48 -0400459 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400461 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400462 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500463 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400464 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 status = -EFAULT;
466 goto err;
467 }
468
469 *levels = obj;
470
Patrick Mocheld550d982006-06-27 00:41:40 -0400471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Len Brown4be44fc2005-08-05 00:44:28 -0400473 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800474 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Patrick Mocheld550d982006-06-27 00:41:40 -0400476 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479static int
Len Brown4be44fc2005-08-05 00:44:28 -0400480acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400482 int status = AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400483 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
484 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400489 if (device->cap._BCM)
490 status = acpi_evaluate_object(device->dev->handle, "_BCM",
491 &args, NULL);
492 device->brightness->curr = level;
Patrick Mocheld550d982006-06-27 00:41:40 -0400493 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496static int
Len Brown4be44fc2005-08-05 00:44:28 -0400497acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400498 unsigned long long *level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400500 if (device->cap._BQC)
501 return acpi_evaluate_integer(device->dev->handle, "_BQC", NULL,
502 level);
503 *level = device->brightness->curr;
504 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507static int
Len Brown4be44fc2005-08-05 00:44:28 -0400508acpi_video_device_EDID(struct acpi_video_device *device,
509 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Len Brown4be44fc2005-08-05 00:44:28 -0400511 int status;
512 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
513 union acpi_object *obj;
514 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
515 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 *edid = NULL;
519
520 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400521 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (length == 128)
523 arg0.integer.value = 1;
524 else if (length == 256)
525 arg0.integer.value = 2;
526 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400527 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Patrick Mochel90130262006-05-19 16:54:48 -0400529 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400531 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200533 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 if (obj && obj->type == ACPI_TYPE_BUFFER)
536 *edid = obj;
537 else {
Len Brown64684632006-06-26 23:41:38 -0400538 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 status = -EFAULT;
540 kfree(obj);
541 }
542
Patrick Mocheld550d982006-06-27 00:41:40 -0400543 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546/* bus */
547
548static int
Len Brown4be44fc2005-08-05 00:44:28 -0400549acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Len Brown4be44fc2005-08-05 00:44:28 -0400551 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400552 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400553 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
554 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 arg0.integer.value = option;
558
Patrick Mochel90130262006-05-19 16:54:48 -0400559 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400561 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Patrick Mocheld550d982006-06-27 00:41:40 -0400563 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400567acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 int status;
570
Patrick Mochel90130262006-05-19 16:54:48 -0400571 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Patrick Mocheld550d982006-06-27 00:41:40 -0400573 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576static int
Len Brown4be44fc2005-08-05 00:44:28 -0400577acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400578 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Len Brown4be44fc2005-08-05 00:44:28 -0400580 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Patrick Mochel90130262006-05-19 16:54:48 -0400582 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 *options &= 3;
584
Patrick Mocheld550d982006-06-27 00:41:40 -0400585 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588/*
589 * Arg:
590 * video : video bus device pointer
591 * bios_flag :
592 * 0. The system BIOS should NOT automatically switch(toggle)
593 * the active display output.
594 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100595 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * 2. The _DGS value should be locked.
597 * 3. The system BIOS should not automatically switch (toggle) the
598 * active display output, but instead generate the display switch
599 * event notify code.
600 * lcd_flag :
601 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100602 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100604 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * Return Value:
606 * -1 wrong arg.
607 */
608
609static int
Len Brown4be44fc2005-08-05 00:44:28 -0400610acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Len Brown4be44fc2005-08-05 00:44:28 -0400612 acpi_integer status = 0;
613 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
614 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Len Brown4be44fc2005-08-05 00:44:28 -0400617 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 status = -1;
619 goto Failed;
620 }
621 arg0.integer.value = (lcd_flag << 2) | bios_flag;
622 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400623 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Len Brown4be44fc2005-08-05 00:44:28 -0400625 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400626 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629/*
630 * Arg:
631 * device : video output device (LCD, CRT, ..)
632 *
633 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100634 * Maximum brightness level
635 *
636 * Allocate and initialize device->brightness.
637 */
638
639static int
640acpi_video_init_brightness(struct acpi_video_device *device)
641{
642 union acpi_object *obj = NULL;
643 int i, max_level = 0, count = 0;
644 union acpi_object *o;
645 struct acpi_video_device_brightness *br = NULL;
646
647 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
648 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
649 "LCD brightness level\n"));
650 goto out;
651 }
652
653 if (obj->package.count < 2)
654 goto out;
655
656 br = kzalloc(sizeof(*br), GFP_KERNEL);
657 if (!br) {
658 printk(KERN_ERR "can't allocate memory\n");
659 goto out;
660 }
661
662 br->levels = kmalloc(obj->package.count * sizeof *(br->levels),
663 GFP_KERNEL);
664 if (!br->levels)
665 goto out_free;
666
667 for (i = 0; i < obj->package.count; i++) {
668 o = (union acpi_object *)&obj->package.elements[i];
669 if (o->type != ACPI_TYPE_INTEGER) {
670 printk(KERN_ERR PREFIX "Invalid data\n");
671 continue;
672 }
673 br->levels[count] = (u32) o->integer.value;
674
675 if (br->levels[count] > max_level)
676 max_level = br->levels[count];
677 count++;
678 }
679
680 if (count < 2)
681 goto out_free_levels;
682
683 br->count = count;
684 device->brightness = br;
685 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "found %d brightness levels\n", count));
686 kfree(obj);
687 return max_level;
688
689out_free_levels:
690 kfree(br->levels);
691out_free:
692 kfree(br);
693out:
694 device->brightness = NULL;
695 kfree(obj);
696 return 0;
697}
698
699/*
700 * Arg:
701 * device : video output device (LCD, CRT, ..)
702 *
703 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 * None
705 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100706 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * device.
708 */
709
Len Brown4be44fc2005-08-05 00:44:28 -0400710static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 acpi_handle h_dummy1;
Yu Luming2f3d0002006-11-11 02:40:34 +0800713 u32 max_level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
William Lee Irwin III98934de2007-12-12 03:56:55 -0800716 memset(&device->cap, 0, sizeof(device->cap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Patrick Mochel90130262006-05-19 16:54:48 -0400718 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 device->cap._ADR = 1;
720 }
Patrick Mochel90130262006-05-19 16:54:48 -0400721 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400722 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Patrick Mochel90130262006-05-19 16:54:48 -0400724 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400725 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800727 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
728 device->cap._BQC = 1;
Patrick Mochel90130262006-05-19 16:54:48 -0400729 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400730 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Patrick Mochel90130262006-05-19 16:54:48 -0400732 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 device->cap._DCS = 1;
734 }
Patrick Mochel90130262006-05-19 16:54:48 -0400735 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 device->cap._DGS = 1;
737 }
Patrick Mochel90130262006-05-19 16:54:48 -0400738 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 device->cap._DSS = 1;
740 }
741
Julia Jomantaite469778c2008-06-23 22:50:42 +0100742 max_level = acpi_video_init_brightness(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Zhao Yakuic2c78902008-07-17 10:46:05 +0800744 if (device->cap._BCL && device->cap._BCM && max_level > 0) {
Zhang Rui702ed512008-01-17 15:51:22 +0800745 int result;
Yu Luming2f3d0002006-11-11 02:40:34 +0800746 static int count = 0;
747 char *name;
Yu Luming2f3d0002006-11-11 02:40:34 +0800748 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
749 if (!name)
750 return;
751
Yu Luming2f3d0002006-11-11 02:40:34 +0800752 sprintf(name, "acpi_video%d", count++);
Yu Luming2f3d0002006-11-11 02:40:34 +0800753 device->backlight = backlight_device_register(name,
Richard Purdie599a52d2007-02-10 23:07:48 +0000754 NULL, device, &acpi_backlight_ops);
Matthew Garrett38531e62007-12-26 02:03:26 +0000755 device->backlight->props.max_brightness = device->brightness->count-3;
Zhao Yakuic2c78902008-07-17 10:46:05 +0800756 /*
757 * If there exists the _BQC object, the _BQC object will be
758 * called to get the current backlight brightness. Otherwise
759 * the brightness will be set to the maximum.
760 */
761 if (device->cap._BQC)
762 device->backlight->props.brightness =
763 acpi_video_get_brightness(device->backlight);
764 else
765 device->backlight->props.brightness =
766 device->backlight->props.max_brightness;
Richard Purdie599a52d2007-02-10 23:07:48 +0000767 backlight_update_status(device->backlight);
Yu Luming2f3d0002006-11-11 02:40:34 +0800768 kfree(name);
Zhang Rui702ed512008-01-17 15:51:22 +0800769
770 device->cdev = thermal_cooling_device_register("LCD",
771 device->dev, &video_cooling_ops);
Thomas Sujith43ff39f2008-02-15 18:29:18 -0500772 if (IS_ERR(device->cdev))
773 return;
774
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200775 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
776 device->cdev->id);
Julia Lawall90300622008-04-11 10:09:24 +0800777 result = sysfs_create_link(&device->dev->dev.kobj,
778 &device->cdev->device.kobj,
779 "thermal_cooling");
780 if (result)
781 printk(KERN_ERR PREFIX "Create sysfs link\n");
782 result = sysfs_create_link(&device->cdev->device.kobj,
783 &device->dev->dev.kobj, "device");
784 if (result)
785 printk(KERN_ERR PREFIX "Create sysfs link\n");
786
Yu Luming2f3d0002006-11-11 02:40:34 +0800787 }
Luming Yu23b0f012007-05-09 21:07:05 +0800788 if (device->cap._DCS && device->cap._DSS){
789 static int count = 0;
790 char *name;
791 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
792 if (!name)
793 return;
794 sprintf(name, "acpi_video%d", count++);
795 device->output_dev = video_output_register(name,
796 NULL, device, &acpi_output_properties);
797 kfree(name);
798 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400799 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
802/*
803 * Arg:
804 * device : video output device (VGA)
805 *
806 * Return Value:
807 * None
808 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100809 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 */
811
Len Brown4be44fc2005-08-05 00:44:28 -0400812static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Len Brown4be44fc2005-08-05 00:44:28 -0400814 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
William Lee Irwin III98934de2007-12-12 03:56:55 -0800816 memset(&video->cap, 0, sizeof(video->cap));
Patrick Mochel90130262006-05-19 16:54:48 -0400817 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 video->cap._DOS = 1;
819 }
Patrick Mochel90130262006-05-19 16:54:48 -0400820 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 video->cap._DOD = 1;
822 }
Patrick Mochel90130262006-05-19 16:54:48 -0400823 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 video->cap._ROM = 1;
825 }
Patrick Mochel90130262006-05-19 16:54:48 -0400826 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 video->cap._GPD = 1;
828 }
Patrick Mochel90130262006-05-19 16:54:48 -0400829 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 video->cap._SPD = 1;
831 }
Patrick Mochel90130262006-05-19 16:54:48 -0400832 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 video->cap._VPO = 1;
834 }
835}
836
837/*
838 * Check whether the video bus device has required AML method to
839 * support the desired features
840 */
841
Len Brown4be44fc2005-08-05 00:44:28 -0400842static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Len Brown4be44fc2005-08-05 00:44:28 -0400844 acpi_status status = -ENOENT;
Thomas Renninger22c13f92008-08-01 17:37:54 +0200845 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400848 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Thomas Renninger22c13f92008-08-01 17:37:54 +0200850 dev = acpi_get_physical_pci_device(video->device->handle);
851 if (!dev)
852 return -ENODEV;
853 put_device(dev);
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 /* Since there is no HID, CID and so on for VGA driver, we have
856 * to check well known required nodes.
857 */
858
Julius Volz98fb8fe2007-02-20 16:38:40 +0100859 /* Does this device support video switching? */
Len Brown4be44fc2005-08-05 00:44:28 -0400860 if (video->cap._DOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 video->flags.multihead = 1;
862 status = 0;
863 }
864
Julius Volz98fb8fe2007-02-20 16:38:40 +0100865 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -0400866 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 video->flags.rom = 1;
868 status = 0;
869 }
870
Julius Volz98fb8fe2007-02-20 16:38:40 +0100871 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -0400872 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 video->flags.post = 1;
874 status = 0;
875 }
876
Patrick Mocheld550d982006-06-27 00:41:40 -0400877 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
880/* --------------------------------------------------------------------------
881 FS Interface (/proc)
882 -------------------------------------------------------------------------- */
883
Len Brown4be44fc2005-08-05 00:44:28 -0400884static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886/* video devices */
887
Len Brown4be44fc2005-08-05 00:44:28 -0400888static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200890 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 if (!dev)
894 goto end;
895
896 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
897 seq_printf(seq, "type: ");
898 if (dev->flags.crt)
899 seq_printf(seq, "CRT\n");
900 else if (dev->flags.lcd)
901 seq_printf(seq, "LCD\n");
902 else if (dev->flags.tvout)
903 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -0500904 else if (dev->flags.dvi)
905 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 else
907 seq_printf(seq, "UNKNOWN\n");
908
Len Brown4be44fc2005-08-05 00:44:28 -0400909 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Len Brown4be44fc2005-08-05 00:44:28 -0400911 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915static int
Len Brown4be44fc2005-08-05 00:44:28 -0400916acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
918 return single_open(file, acpi_video_device_info_seq_show,
919 PDE(inode)->data);
920}
921
Len Brown4be44fc2005-08-05 00:44:28 -0400922static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Len Brown4be44fc2005-08-05 00:44:28 -0400924 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200925 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400926 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 if (!dev)
930 goto end;
931
932 status = acpi_video_device_get_state(dev, &state);
933 seq_printf(seq, "state: ");
934 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -0400935 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 else
937 seq_printf(seq, "<not supported>\n");
938
939 status = acpi_video_device_query(dev, &state);
940 seq_printf(seq, "query: ");
941 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -0400942 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 else
944 seq_printf(seq, "<not supported>\n");
945
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_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
953 return single_open(file, acpi_video_device_state_seq_show,
954 PDE(inode)->data);
955}
956
957static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400958acpi_video_device_write_state(struct file *file,
959 const char __user * buffer,
960 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Len Brown4be44fc2005-08-05 00:44:28 -0400962 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200963 struct seq_file *m = file->private_data;
964 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400965 char str[12] = { 0 };
966 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 if (!dev || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -0400970 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400973 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 str[count] = 0;
976 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -0400977 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 status = acpi_video_device_set_state(dev, state);
980
981 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -0400982 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Patrick Mocheld550d982006-06-27 00:41:40 -0400984 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
987static int
Len Brown4be44fc2005-08-05 00:44:28 -0400988acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200990 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400991 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 if (!dev || !dev->brightness) {
995 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998
999 seq_printf(seq, "levels: ");
1000 for (i = 0; i < dev->brightness->count; i++)
1001 seq_printf(seq, " %d", dev->brightness->levels[i]);
1002 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1003
Patrick Mocheld550d982006-06-27 00:41:40 -04001004 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
1007static int
Len Brown4be44fc2005-08-05 00:44:28 -04001008acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 return single_open(file, acpi_video_device_brightness_seq_show,
1011 PDE(inode)->data);
1012}
1013
1014static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001015acpi_video_device_write_brightness(struct file *file,
1016 const char __user * buffer,
1017 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001019 struct seq_file *m = file->private_data;
1020 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001021 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001022 unsigned int level = 0;
1023 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Thomas Renninger59d399d2005-11-08 05:27:00 -05001026 if (!dev || !dev->brightness || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001027 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001030 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 str[count] = 0;
1033 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001036 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Julius Volz98fb8fe2007-02-20 16:38:40 +01001038 /* validate through the list of available levels */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 for (i = 0; i < dev->brightness->count; i++)
1040 if (level == dev->brightness->levels[i]) {
Len Brown4be44fc2005-08-05 00:44:28 -04001041 if (ACPI_SUCCESS
1042 (acpi_video_device_lcd_set_level(dev, level)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 dev->brightness->curr = level;
1044 break;
1045 }
1046
Patrick Mocheld550d982006-06-27 00:41:40 -04001047 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Len Brown4be44fc2005-08-05 00:44:28 -04001050static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001052 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001053 int status;
1054 int i;
1055 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
1058 if (!dev)
1059 goto out;
1060
Len Brown4be44fc2005-08-05 00:44:28 -04001061 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001063 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
1066 if (ACPI_FAILURE(status)) {
1067 goto out;
1068 }
1069
1070 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1071 for (i = 0; i < edid->buffer.length; i++)
1072 seq_putc(seq, edid->buffer.pointer[i]);
1073 }
1074
Len Brown4be44fc2005-08-05 00:44:28 -04001075 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (!edid)
1077 seq_printf(seq, "<not supported>\n");
1078 else
1079 kfree(edid);
1080
Patrick Mocheld550d982006-06-27 00:41:40 -04001081 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
1084static int
Len Brown4be44fc2005-08-05 00:44:28 -04001085acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
1087 return single_open(file, acpi_video_device_EDID_seq_show,
1088 PDE(inode)->data);
1089}
1090
Len Brown4be44fc2005-08-05 00:44:28 -04001091static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001093 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct acpi_video_device *vid_dev;
1095
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001096 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001098 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001100 device_dir = proc_mkdir(acpi_device_bid(device),
1101 vid_dev->video->dir);
1102 if (!device_dir)
1103 return -ENOMEM;
1104
1105 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001108 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001109 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001111 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001114 acpi_video_device_state_fops.write = acpi_video_device_write_state;
1115 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001116 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001117 &acpi_video_device_state_fops,
1118 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001120 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001123 acpi_video_device_brightness_fops.write =
1124 acpi_video_device_write_brightness;
1125 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001126 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001127 &acpi_video_device_brightness_fops,
1128 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001130 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001133 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001134 &acpi_video_device_EDID_fops,
1135 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001137 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001139 acpi_device_dir(device) = device_dir;
1140
Patrick Mocheld550d982006-06-27 00:41:40 -04001141 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001142
1143 err_remove_brightness:
1144 remove_proc_entry("brightness", device_dir);
1145 err_remove_state:
1146 remove_proc_entry("state", device_dir);
1147 err_remove_info:
1148 remove_proc_entry("info", device_dir);
1149 err_remove_dir:
1150 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1151 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
Len Brown4be44fc2005-08-05 00:44:28 -04001154static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
1156 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001157 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001159 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001161 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001163 device_dir = acpi_device_dir(device);
1164 if (device_dir) {
1165 remove_proc_entry("info", device_dir);
1166 remove_proc_entry("state", device_dir);
1167 remove_proc_entry("brightness", device_dir);
1168 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001169 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 acpi_device_dir(device) = NULL;
1171 }
1172
Patrick Mocheld550d982006-06-27 00:41:40 -04001173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001177static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001179 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 if (!video)
1183 goto end;
1184
1185 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001186 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001188 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001190 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Len Brown4be44fc2005-08-05 00:44:28 -04001192 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Len Brown4be44fc2005-08-05 00:44:28 -04001196static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Len Brown4be44fc2005-08-05 00:44:28 -04001198 return single_open(file, acpi_video_bus_info_seq_show,
1199 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Len Brown4be44fc2005-08-05 00:44:28 -04001202static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001204 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 if (!video)
1208 goto end;
1209
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001210 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 seq_printf(seq, "<TODO>\n");
1212
Len Brown4be44fc2005-08-05 00:44:28 -04001213 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Len Brown4be44fc2005-08-05 00:44:28 -04001217static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1220}
1221
Len Brown4be44fc2005-08-05 00:44:28 -04001222static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001224 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001225 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001226 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 if (!video)
1230 goto end;
1231
1232 status = acpi_video_bus_POST_options(video, &options);
1233 if (ACPI_SUCCESS(status)) {
1234 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001235 printk(KERN_WARNING PREFIX
1236 "The motherboard VGA device is not listed as a possible POST device.\n");
1237 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001238 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Matthew Wilcox27663c52008-10-10 02:22:59 -04001240 printk("%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001241 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (options & 2)
1243 seq_printf(seq, " <PCI video>");
1244 if (options & 4)
1245 seq_printf(seq, " <AGP video>");
1246 seq_putc(seq, '\n');
1247 } else
1248 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001249 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
1253static int
Len Brown4be44fc2005-08-05 00:44:28 -04001254acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Len Brown4be44fc2005-08-05 00:44:28 -04001256 return single_open(file, acpi_video_bus_POST_info_seq_show,
1257 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Len Brown4be44fc2005-08-05 00:44:28 -04001260static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001262 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001263 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001264 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 if (!video)
1268 goto end;
1269
Len Brown4be44fc2005-08-05 00:44:28 -04001270 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 if (!ACPI_SUCCESS(status)) {
1272 seq_printf(seq, "<not supported>\n");
1273 goto end;
1274 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001275 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Len Brown4be44fc2005-08-05 00:44:28 -04001277 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
Len Brown4be44fc2005-08-05 00:44:28 -04001281static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001283 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Len Brown4be44fc2005-08-05 00:44:28 -04001286 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Patrick Mocheld550d982006-06-27 00:41:40 -04001288 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289}
1290
Len Brown4be44fc2005-08-05 00:44:28 -04001291static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Len Brown4be44fc2005-08-05 00:44:28 -04001293 return single_open(file, acpi_video_bus_POST_seq_show,
1294 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
Len Brown4be44fc2005-08-05 00:44:28 -04001297static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
1299 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1300}
1301
1302static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001303acpi_video_bus_write_POST(struct file *file,
1304 const char __user * buffer,
1305 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Len Brown4be44fc2005-08-05 00:44:28 -04001307 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001308 struct seq_file *m = file->private_data;
1309 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001310 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001311 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001315 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 status = acpi_video_bus_POST_options(video, &options);
1318 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001319 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001322 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 str[count] = 0;
1325 opt = strtoul(str, NULL, 0);
1326 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001327 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Julius Volz98fb8fe2007-02-20 16:38:40 +01001329 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 options |= 1;
1331
1332 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001333 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001335 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 }
1338
Patrick Mocheld550d982006-06-27 00:41:40 -04001339 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
1342static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001343acpi_video_bus_write_DOS(struct file *file,
1344 const char __user * buffer,
1345 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Len Brown4be44fc2005-08-05 00:44:28 -04001347 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001348 struct seq_file *m = file->private_data;
1349 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001350 char str[12] = { 0 };
1351 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001355 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001358 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 str[count] = 0;
1361 opt = strtoul(str, NULL, 0);
1362 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001363 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Len Brown4be44fc2005-08-05 00:44:28 -04001365 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001368 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Patrick Mocheld550d982006-06-27 00:41:40 -04001370 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
Len Brown4be44fc2005-08-05 00:44:28 -04001373static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001375 struct acpi_video_bus *video = acpi_driver_data(device);
1376 struct proc_dir_entry *device_dir;
1377 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001379 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1380 if (!device_dir)
1381 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001383 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001386 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001387 &acpi_video_bus_info_fops,
1388 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001390 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001393 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001394 &acpi_video_bus_ROM_fops,
1395 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001397 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001400 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001401 &acpi_video_bus_POST_info_fops,
1402 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001404 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 /* 'POST' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001407 acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
Linus Torvalds08acd4f2008-04-30 11:52:52 -07001408 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001409 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001410 &acpi_video_bus_POST_fops,
1411 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001413 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 /* 'DOS' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001416 acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
Linus Torvalds08acd4f2008-04-30 11:52:52 -07001417 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001418 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001419 &acpi_video_bus_DOS_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_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001424 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001425 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001426
1427 err_remove_post:
1428 remove_proc_entry("POST", device_dir);
1429 err_remove_post_info:
1430 remove_proc_entry("POST_info", device_dir);
1431 err_remove_rom:
1432 remove_proc_entry("ROM", device_dir);
1433 err_remove_info:
1434 remove_proc_entry("info", device_dir);
1435 err_remove_dir:
1436 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1437 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
1439
Len Brown4be44fc2005-08-05 00:44:28 -04001440static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001442 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001444 if (device_dir) {
1445 remove_proc_entry("info", device_dir);
1446 remove_proc_entry("ROM", device_dir);
1447 remove_proc_entry("POST_info", device_dir);
1448 remove_proc_entry("POST", device_dir);
1449 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001450 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 acpi_device_dir(device) = NULL;
1452 }
1453
Patrick Mocheld550d982006-06-27 00:41:40 -04001454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
1457/* --------------------------------------------------------------------------
1458 Driver Interface
1459 -------------------------------------------------------------------------- */
1460
1461/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001462static struct acpi_video_device_attrib*
1463acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1464{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001465 struct acpi_video_enumerated_device *ids;
1466 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001467
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001468 for (i = 0; i < video->attached_count; i++) {
1469 ids = &video->attached_array[i];
1470 if ((ids->value.int_val & 0xffff) == device_id)
1471 return &ids->value.attrib;
1472 }
1473
Rui Zhang82cae992007-01-03 23:40:53 -05001474 return NULL;
1475}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477static int
Len Brown4be44fc2005-08-05 00:44:28 -04001478acpi_video_bus_get_one_device(struct acpi_device *device,
1479 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001481 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001482 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001483 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001484 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001487 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Len Brown4be44fc2005-08-05 00:44:28 -04001489 status =
1490 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (ACPI_SUCCESS(status)) {
1492
Burman Yan36bcbec2006-12-19 12:56:11 -08001493 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001495 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1498 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001499 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 data->device_id = device_id;
1502 data->video = video;
1503 data->dev = device;
1504
Rui Zhang82cae992007-01-03 23:40:53 -05001505 attribute = acpi_video_get_device_attr(video, device_id);
1506
1507 if((attribute != NULL) && attribute->device_id_scheme) {
1508 switch (attribute->display_type) {
1509 case ACPI_VIDEO_DISPLAY_CRT:
1510 data->flags.crt = 1;
1511 break;
1512 case ACPI_VIDEO_DISPLAY_TV:
1513 data->flags.tvout = 1;
1514 break;
1515 case ACPI_VIDEO_DISPLAY_DVI:
1516 data->flags.dvi = 1;
1517 break;
1518 case ACPI_VIDEO_DISPLAY_LCD:
1519 data->flags.lcd = 1;
1520 break;
1521 default:
1522 data->flags.unknown = 1;
1523 break;
1524 }
1525 if(attribute->bios_can_detect)
1526 data->flags.bios = 1;
1527 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 acpi_video_device_bind(video, data);
1531 acpi_video_device_find_cap(data);
1532
Patrick Mochel90130262006-05-19 16:54:48 -04001533 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001534 ACPI_DEVICE_NOTIFY,
1535 acpi_video_device_notify,
1536 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001538 printk(KERN_ERR PREFIX
1539 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001540 if(data->brightness)
1541 kfree(data->brightness->levels);
1542 kfree(data->brightness);
1543 kfree(data);
1544 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001547 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001549 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 acpi_video_device_add_fs(device);
1552
Patrick Mocheld550d982006-06-27 00:41:40 -04001553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 }
1555
Patrick Mocheld550d982006-06-27 00:41:40 -04001556 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
1559/*
1560 * Arg:
1561 * video : video bus device
1562 *
1563 * Return:
1564 * none
1565 *
1566 * Enumerate the video device list of the video bus,
1567 * bind the ids with the corresponding video devices
1568 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001569 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Len Brown4be44fc2005-08-05 00:44:28 -04001571static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001573 struct acpi_video_device *dev;
1574
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001575 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001576
1577 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001578 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001579
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001580 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
1583/*
1584 * Arg:
1585 * video : video bus device
1586 * device : video output device under the video
1587 * bus
1588 *
1589 * Return:
1590 * none
1591 *
1592 * Bind the ids with the corresponding video devices
1593 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001594 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596static void
Len Brown4be44fc2005-08-05 00:44:28 -04001597acpi_video_device_bind(struct acpi_video_bus *video,
1598 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001600 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001601 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001603 for (i = 0; i < video->attached_count; i++) {
1604 ids = &video->attached_array[i];
1605 if (device->device_id == (ids->value.int_val & 0xffff)) {
1606 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1608 }
1609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
1612/*
1613 * Arg:
1614 * video : video bus device
1615 *
1616 * Return:
1617 * < 0 : error
1618 *
1619 * Call _DOD to enumerate all devices attached to display adapter
1620 *
Len Brown4be44fc2005-08-05 00:44:28 -04001621 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1624{
Len Brown4be44fc2005-08-05 00:44:28 -04001625 int status;
1626 int count;
1627 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001628 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001629 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1630 union acpi_object *dod = NULL;
1631 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Patrick Mochel90130262006-05-19 16:54:48 -04001633 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001635 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001636 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001639 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001641 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 status = -EFAULT;
1643 goto out;
1644 }
1645
1646 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001647 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001649 active_list = kcalloc(1 + dod->package.count,
1650 sizeof(struct acpi_video_enumerated_device),
1651 GFP_KERNEL);
1652 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 status = -ENOMEM;
1654 goto out;
1655 }
1656
1657 count = 0;
1658 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001659 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001662 printk(KERN_ERR PREFIX
1663 "Invalid _DOD data in element %d\n", i);
1664 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001666
1667 active_list[count].value.int_val = obj->integer.value;
1668 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001669 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1670 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 count++;
1672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Jesper Juhl6044ec82005-11-07 01:01:32 -08001674 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001675
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001676 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001678
1679 out:
Len Brown02438d82006-06-30 03:19:10 -04001680 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001681 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
Len Brown4be44fc2005-08-05 00:44:28 -04001684static int
1685acpi_video_get_next_level(struct acpi_video_device *device,
1686 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001688 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001689 max = max_below = 0;
1690 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001691 /* Find closest level to level_current */
1692 for (i = 0; i < device->brightness->count; i++) {
1693 l = device->brightness->levels[i];
1694 if (abs(l - level_current) < abs(delta)) {
1695 delta = l - level_current;
1696 if (!delta)
1697 break;
1698 }
1699 }
1700 /* Ajust level_current to closest available level */
1701 level_current += delta;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001702 for (i = 0; i < device->brightness->count; i++) {
1703 l = device->brightness->levels[i];
1704 if (l < min)
1705 min = l;
1706 if (l > max)
1707 max = l;
1708 if (l < min_above && l > level_current)
1709 min_above = l;
1710 if (l > max_below && l < level_current)
1711 max_below = l;
1712 }
1713
1714 switch (event) {
1715 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1716 return (level_current < max) ? min_above : min;
1717 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1718 return (level_current < max) ? min_above : max;
1719 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1720 return (level_current > min) ? max_below : min;
1721 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1722 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1723 return 0;
1724 default:
1725 return level_current;
1726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729static void
Len Brown4be44fc2005-08-05 00:44:28 -04001730acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001732 unsigned long long level_current, level_next;
Julia Jomantaite469778c2008-06-23 22:50:42 +01001733 if (!device->brightness)
1734 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 acpi_video_device_lcd_get_level_current(device, &level_current);
1736 level_next = acpi_video_get_next_level(device, level_current, event);
1737 acpi_video_device_lcd_set_level(device, level_next);
1738}
1739
1740static int
Len Brown4be44fc2005-08-05 00:44:28 -04001741acpi_video_bus_get_devices(struct acpi_video_bus *video,
1742 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Len Brown4be44fc2005-08-05 00:44:28 -04001744 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001745 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 acpi_video_device_enumerate(video);
1748
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001749 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 status = acpi_video_bus_get_one_device(dev, video);
1752 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001753 printk(KERN_WARNING PREFIX
1754 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 continue;
1756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001758 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
Len Brown4be44fc2005-08-05 00:44:28 -04001761static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Karol Kozimor031ec772005-07-30 04:18:00 -04001763 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 struct acpi_video_bus *video;
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001768 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 video = device->video;
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 acpi_video_device_remove_fs(device->dev);
1773
Patrick Mochel90130262006-05-19 16:54:48 -04001774 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001775 ACPI_DEVICE_NOTIFY,
1776 acpi_video_device_notify);
Richard Purdie599a52d2007-02-10 23:07:48 +00001777 backlight_device_unregister(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +08001778 if (device->cdev) {
1779 sysfs_remove_link(&device->dev->dev.kobj,
1780 "thermal_cooling");
1781 sysfs_remove_link(&device->cdev->device.kobj,
1782 "device");
1783 thermal_cooling_device_unregister(device->cdev);
1784 device->cdev = NULL;
1785 }
Luming Yu23b0f012007-05-09 21:07:05 +08001786 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001787
Patrick Mocheld550d982006-06-27 00:41:40 -04001788 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
Len Brown4be44fc2005-08-05 00:44:28 -04001791static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Len Brown4be44fc2005-08-05 00:44:28 -04001793 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001794 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001796 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001798 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001800 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04001801 if (ACPI_FAILURE(status))
1802 printk(KERN_WARNING PREFIX
1803 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001805 if (dev->brightness) {
1806 kfree(dev->brightness->levels);
1807 kfree(dev->brightness);
1808 }
1809 list_del(&dev->entry);
1810 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001813 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001814
Patrick Mocheld550d982006-06-27 00:41:40 -04001815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
1818/* acpi_video interface */
1819
Len Brown4be44fc2005-08-05 00:44:28 -04001820static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Zhang Ruia21101c2007-09-14 11:46:22 +08001822 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
Len Brown4be44fc2005-08-05 00:44:28 -04001825static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
1827 return acpi_video_bus_DOS(video, 0, 1);
1828}
1829
Len Brown4be44fc2005-08-05 00:44:28 -04001830static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001832 struct acpi_video_bus *video = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001833 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001834 struct input_dev *input;
1835 int keycode;
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001838 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001840 device = video->device;
Luming Yue9dab192007-08-20 18:23:53 +08001841 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001844 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04001846 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001847 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 break;
1849
Julius Volz98fb8fe2007-02-20 16:38:40 +01001850 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 * connector. */
1852 acpi_video_device_enumerate(video);
1853 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04001854 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001855 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 break;
1857
Len Brown4be44fc2005-08-05 00:44:28 -04001858 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001859 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001860 keycode = KEY_SWITCHVIDEOMODE;
1861 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001862 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001863 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001864 keycode = KEY_VIDEO_NEXT;
1865 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001866 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04001867 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001868 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 break;
1870
1871 default:
Luming Yue9dab192007-08-20 18:23:53 +08001872 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001874 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 break;
1876 }
1877
Zhang Rui7761f632008-01-25 14:48:12 +08001878 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001879 input_report_key(input, keycode, 1);
1880 input_sync(input);
1881 input_report_key(input, keycode, 0);
1882 input_sync(input);
1883
Patrick Mocheld550d982006-06-27 00:41:40 -04001884 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Len Brown4be44fc2005-08-05 00:44:28 -04001887static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001889 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001890 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001891 struct acpi_video_bus *bus;
1892 struct input_dev *input;
1893 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001896 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001898 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001899 bus = video_device->video;
1900 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001903 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001904 if (brightness_switch_enabled)
1905 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001906 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001907 keycode = KEY_BRIGHTNESS_CYCLE;
1908 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001909 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001910 if (brightness_switch_enabled)
1911 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001912 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001913 keycode = KEY_BRIGHTNESSUP;
1914 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001915 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001916 if (brightness_switch_enabled)
1917 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001918 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001919 keycode = KEY_BRIGHTNESSDOWN;
1920 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001921 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08001922 if (brightness_switch_enabled)
1923 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001924 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001925 keycode = KEY_BRIGHTNESS_ZERO;
1926 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001927 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08001928 if (brightness_switch_enabled)
1929 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04001930 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001931 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 break;
1933 default:
Luming Yue9dab192007-08-20 18:23:53 +08001934 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001936 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 break;
1938 }
Luming Yue9dab192007-08-20 18:23:53 +08001939
Zhang Rui7761f632008-01-25 14:48:12 +08001940 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001941 input_report_key(input, keycode, 1);
1942 input_sync(input);
1943 input_report_key(input, keycode, 0);
1944 input_sync(input);
1945
Patrick Mocheld550d982006-06-27 00:41:40 -04001946 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
1948
Zhang Ruie6d9da12007-08-25 02:23:31 -04001949static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08001950static int acpi_video_resume(struct acpi_device *device)
1951{
1952 struct acpi_video_bus *video;
1953 struct acpi_video_device *video_device;
1954 int i;
1955
1956 if (!device || !acpi_driver_data(device))
1957 return -EINVAL;
1958
1959 video = acpi_driver_data(device);
1960
1961 for (i = 0; i < video->attached_count; i++) {
1962 video_device = video->attached_array[i].bind_info;
1963 if (video_device && video_device->backlight)
1964 acpi_video_set_brightness(video_device->backlight);
1965 }
1966 return AE_OK;
1967}
1968
Len Brown4be44fc2005-08-05 00:44:28 -04001969static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001971 acpi_status status;
1972 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08001973 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001974 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Burman Yan36bcbec2006-12-19 12:56:11 -08001976 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001978 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Zhang Ruie6d9da12007-08-25 02:23:31 -04001980 /* a hack to fix the duplicate name "VID" problem on T61 */
1981 if (!strcmp(device->pnp.bus_id, "VID")) {
1982 if (instance)
1983 device->pnp.bus_id[3] = '0' + instance;
1984 instance ++;
1985 }
1986
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001987 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1989 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001990 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001993 error = acpi_video_bus_check(video);
1994 if (error)
1995 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001997 error = acpi_video_bus_add_fs(device);
1998 if (error)
1999 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002001 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 INIT_LIST_HEAD(&video->video_device_list);
2003
2004 acpi_video_bus_get_devices(video, device);
2005 acpi_video_bus_start_devices(video);
2006
Patrick Mochel90130262006-05-19 16:54:48 -04002007 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002008 ACPI_DEVICE_NOTIFY,
2009 acpi_video_bus_notify, video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002011 printk(KERN_ERR PREFIX
2012 "Error installing notify handler\n");
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002013 error = -ENODEV;
2014 goto err_stop_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
2016
Luming Yue9dab192007-08-20 18:23:53 +08002017 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002018 if (!input) {
2019 error = -ENOMEM;
2020 goto err_uninstall_notify;
2021 }
Luming Yue9dab192007-08-20 18:23:53 +08002022
2023 snprintf(video->phys, sizeof(video->phys),
2024 "%s/video/input0", acpi_device_hid(video->device));
2025
2026 input->name = acpi_device_name(video->device);
2027 input->phys = video->phys;
2028 input->id.bustype = BUS_HOST;
2029 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002030 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002031 input->evbit[0] = BIT(EV_KEY);
2032 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2033 set_bit(KEY_VIDEO_NEXT, input->keybit);
2034 set_bit(KEY_VIDEO_PREV, input->keybit);
2035 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2036 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2037 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2038 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2039 set_bit(KEY_DISPLAY_OFF, input->keybit);
2040 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002041
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002042 error = input_register_device(input);
2043 if (error)
2044 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002047 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2048 video->flags.multihead ? "yes" : "no",
2049 video->flags.rom ? "yes" : "no",
2050 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002052 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002054 err_free_input_dev:
2055 input_free_device(input);
2056 err_uninstall_notify:
2057 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
2058 acpi_video_bus_notify);
2059 err_stop_video:
2060 acpi_video_bus_stop_devices(video);
2061 acpi_video_bus_put_devices(video);
2062 kfree(video->attached_array);
2063 acpi_video_bus_remove_fs(device);
2064 err_free_video:
2065 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002066 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002067
2068 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
Len Brown4be44fc2005-08-05 00:44:28 -04002071static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Len Brown4be44fc2005-08-05 00:44:28 -04002073 acpi_status status = 0;
2074 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002078 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002080 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 acpi_video_bus_stop_devices(video);
2083
Patrick Mochel90130262006-05-19 16:54:48 -04002084 status = acpi_remove_notify_handler(video->device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002085 ACPI_DEVICE_NOTIFY,
2086 acpi_video_bus_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 acpi_video_bus_put_devices(video);
2089 acpi_video_bus_remove_fs(device);
2090
Luming Yue9dab192007-08-20 18:23:53 +08002091 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002092 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 kfree(video);
2094
Patrick Mocheld550d982006-06-27 00:41:40 -04002095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
Len Brown4be44fc2005-08-05 00:44:28 -04002098static int __init acpi_video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Len Brown4be44fc2005-08-05 00:44:28 -04002100 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 /*
Len Brown4be44fc2005-08-05 00:44:28 -04002104 acpi_dbg_level = 0xFFFFFFFF;
2105 acpi_dbg_layer = 0x08000000;
2106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2109 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002110 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 acpi_video_dir->owner = THIS_MODULE;
2112
2113 result = acpi_bus_register_driver(&acpi_video_bus);
2114 if (result < 0) {
2115 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002116 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
2118
Patrick Mocheld550d982006-06-27 00:41:40 -04002119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Len Brown4be44fc2005-08-05 00:44:28 -04002122static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 acpi_bus_unregister_driver(&acpi_video_bus);
2126
2127 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2128
Patrick Mocheld550d982006-06-27 00:41:40 -04002129 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
2132module_init(acpi_video_init);
2133module_exit(acpi_video_exit);