blob: baa4419297209efd6d79aaedb2dae13c2d1a6f92 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define ACPI_VIDEO_CLASS "video"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define ACPI_VIDEO_BUS_NAME "Video Bus"
46#define ACPI_VIDEO_DEVICE_NAME "Video Device"
47#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
48#define ACPI_VIDEO_NOTIFY_PROBE 0x81
49#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
50#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
51#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
52
Thomas Tuttlef4715182006-12-19 12:56:14 -080053#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
54#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
55#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
56#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
57#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Yu Luming2f3d0002006-11-11 02:40:34 +080059#define MAX_NAME_LEN 20
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Rui Zhang82cae992007-01-03 23:40:53 -050061#define ACPI_VIDEO_DISPLAY_CRT 1
62#define ACPI_VIDEO_DISPLAY_TV 2
63#define ACPI_VIDEO_DISPLAY_DVI 3
64#define ACPI_VIDEO_DISPLAY_LCD 4
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define _COMPONENT ACPI_VIDEO_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050067ACPI_MODULE_NAME("video");
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Len Brownf52fd662007-02-12 22:42:12 -050069MODULE_AUTHOR("Bruno Ducrot");
Len Brown7cda93e2007-02-12 23:50:02 -050070MODULE_DESCRIPTION("ACPI Video Driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070071MODULE_LICENSE("GPL");
72
Zhang Rui8a681a42008-01-25 14:47:49 +080073static int brightness_switch_enabled = 1;
74module_param(brightness_switch_enabled, bool, 0644);
75
Len Brown4be44fc2005-08-05 00:44:28 -040076static int acpi_video_bus_add(struct acpi_device *device);
77static int acpi_video_bus_remove(struct acpi_device *device, int type);
Matthew Garrett863c1492008-02-04 23:31:24 -080078static int acpi_video_resume(struct acpi_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Thomas Renninger1ba90e32007-07-23 14:44:41 +020080static const struct acpi_device_id video_device_ids[] = {
81 {ACPI_VIDEO_HID, 0},
82 {"", 0},
83};
84MODULE_DEVICE_TABLE(acpi, video_device_ids);
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static struct acpi_driver acpi_video_bus = {
Len Brownc2b67052007-02-12 23:33:40 -050087 .name = "video",
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 .class = ACPI_VIDEO_CLASS,
Thomas Renninger1ba90e32007-07-23 14:44:41 +020089 .ids = video_device_ids,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 .ops = {
91 .add = acpi_video_bus_add,
92 .remove = acpi_video_bus_remove,
Matthew Garrett863c1492008-02-04 23:31:24 -080093 .resume = acpi_video_resume,
Len Brown4be44fc2005-08-05 00:44:28 -040094 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
97struct acpi_video_bus_flags {
Len Brown4be44fc2005-08-05 00:44:28 -040098 u8 multihead:1; /* can switch video heads */
99 u8 rom:1; /* can retrieve a video rom */
100 u8 post:1; /* can configure the head to */
101 u8 reserved:5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
104struct acpi_video_bus_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400105 u8 _DOS:1; /*Enable/Disable output switching */
106 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
107 u8 _ROM:1; /*Get ROM Data */
108 u8 _GPD:1; /*Get POST Device */
109 u8 _SPD:1; /*Set POST Device */
110 u8 _VPO:1; /*Video POST Options */
111 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Len Brown4be44fc2005-08-05 00:44:28 -0400114struct acpi_video_device_attrib {
115 u32 display_index:4; /* A zero-based instance of the Display */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100116 u32 display_port_attachment:4; /*This field differentiates the display type */
Len Brown4be44fc2005-08-05 00:44:28 -0400117 u32 display_type:4; /*Describe the specific type in use */
Julius Volz98fb8fe2007-02-20 16:38:40 +0100118 u32 vendor_specific:4; /*Chipset Vendor Specific */
Len Brown4be44fc2005-08-05 00:44:28 -0400119 u32 bios_can_detect:1; /*BIOS can detect the device */
120 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
121 the VGA device. */
122 u32 pipe_id:3; /*For VGA multiple-head devices. */
123 u32 reserved:10; /*Must be 0 */
124 u32 device_id_scheme:1; /*Device ID Scheme */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127struct acpi_video_enumerated_device {
128 union {
129 u32 int_val;
Len Brown4be44fc2005-08-05 00:44:28 -0400130 struct acpi_video_device_attrib attrib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 } value;
132 struct acpi_video_device *bind_info;
133};
134
135struct acpi_video_bus {
Patrick Mochele6afa0d2006-05-19 16:54:40 -0400136 struct acpi_device *device;
Len Brown4be44fc2005-08-05 00:44:28 -0400137 u8 dos_setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 struct acpi_video_enumerated_device *attached_array;
Len Brown4be44fc2005-08-05 00:44:28 -0400139 u8 attached_count;
140 struct acpi_video_bus_cap cap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct acpi_video_bus_flags flags;
Len Brown4be44fc2005-08-05 00:44:28 -0400142 struct list_head video_device_list;
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -0500143 struct mutex device_list_lock; /* protects video_device_list */
Len Brown4be44fc2005-08-05 00:44:28 -0400144 struct proc_dir_entry *dir;
Luming Yue9dab192007-08-20 18:23:53 +0800145 struct input_dev *input;
146 char phys[32]; /* for input device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
149struct acpi_video_device_flags {
Len Brown4be44fc2005-08-05 00:44:28 -0400150 u8 crt:1;
151 u8 lcd:1;
152 u8 tvout:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500153 u8 dvi:1;
Len Brown4be44fc2005-08-05 00:44:28 -0400154 u8 bios:1;
155 u8 unknown:1;
Rui Zhang82cae992007-01-03 23:40:53 -0500156 u8 reserved:2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157};
158
159struct acpi_video_device_cap {
Len Brown4be44fc2005-08-05 00:44:28 -0400160 u8 _ADR:1; /*Return the unique ID */
161 u8 _BCL:1; /*Query list of brightness control levels supported */
162 u8 _BCM:1; /*Set the brightness level */
Yu Luming2f3d0002006-11-11 02:40:34 +0800163 u8 _BQC:1; /* Get current brightness level */
Len Brown4be44fc2005-08-05 00:44:28 -0400164 u8 _DDC:1; /*Return the EDID for this device */
165 u8 _DCS:1; /*Return status of output device */
166 u8 _DGS:1; /*Query graphics state */
167 u8 _DSS:1; /*Device state set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168};
169
170struct acpi_video_device_brightness {
Len Brown4be44fc2005-08-05 00:44:28 -0400171 int curr;
172 int count;
173 int *levels;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174};
175
176struct acpi_video_device {
Len Brown4be44fc2005-08-05 00:44:28 -0400177 unsigned long device_id;
178 struct acpi_video_device_flags flags;
179 struct acpi_video_device_cap cap;
180 struct list_head entry;
181 struct acpi_video_bus *video;
182 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 struct acpi_video_device_brightness *brightness;
Yu Luming2f3d0002006-11-11 02:40:34 +0800184 struct backlight_device *backlight;
Zhang Rui702ed512008-01-17 15:51:22 +0800185 struct thermal_cooling_device *cdev;
Luming Yu23b0f012007-05-09 21:07:05 +0800186 struct output_device *output_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/* bus */
190static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
191static struct file_operations acpi_video_bus_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700192 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400193 .open = acpi_video_bus_info_open_fs,
194 .read = seq_read,
195 .llseek = seq_lseek,
196 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197};
198
199static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
200static struct file_operations acpi_video_bus_ROM_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700201 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400202 .open = acpi_video_bus_ROM_open_fs,
203 .read = seq_read,
204 .llseek = seq_lseek,
205 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206};
207
Len Brown4be44fc2005-08-05 00:44:28 -0400208static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
209 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static struct file_operations acpi_video_bus_POST_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700211 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400212 .open = acpi_video_bus_POST_info_open_fs,
213 .read = seq_read,
214 .llseek = seq_lseek,
215 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
218static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
219static struct file_operations acpi_video_bus_POST_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700220 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400221 .open = acpi_video_bus_POST_open_fs,
222 .read = seq_read,
223 .llseek = seq_lseek,
224 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225};
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
228static struct file_operations acpi_video_bus_DOS_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700229 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400230 .open = acpi_video_bus_DOS_open_fs,
231 .read = seq_read,
232 .llseek = seq_lseek,
233 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
236/* device */
Len Brown4be44fc2005-08-05 00:44:28 -0400237static int acpi_video_device_info_open_fs(struct inode *inode,
238 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static struct file_operations acpi_video_device_info_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700240 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400241 .open = acpi_video_device_info_open_fs,
242 .read = seq_read,
243 .llseek = seq_lseek,
244 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
Len Brown4be44fc2005-08-05 00:44:28 -0400247static int acpi_video_device_state_open_fs(struct inode *inode,
248 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249static struct file_operations acpi_video_device_state_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700250 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400251 .open = acpi_video_device_state_open_fs,
252 .read = seq_read,
253 .llseek = seq_lseek,
254 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
Len Brown4be44fc2005-08-05 00:44:28 -0400257static int acpi_video_device_brightness_open_fs(struct inode *inode,
258 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259static struct file_operations acpi_video_device_brightness_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700260 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400261 .open = acpi_video_device_brightness_open_fs,
262 .read = seq_read,
263 .llseek = seq_lseek,
264 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265};
266
Len Brown4be44fc2005-08-05 00:44:28 -0400267static int acpi_video_device_EDID_open_fs(struct inode *inode,
268 struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269static struct file_operations acpi_video_device_EDID_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -0700270 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -0400271 .open = acpi_video_device_EDID_open_fs,
272 .read = seq_read,
273 .llseek = seq_lseek,
274 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275};
276
Len Brown4be44fc2005-08-05 00:44:28 -0400277static char device_decode[][30] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 "motherboard VGA device",
279 "PCI VGA device",
280 "AGP VGA device",
281 "UNKNOWN",
282};
283
Len Brown4be44fc2005-08-05 00:44:28 -0400284static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
285static void acpi_video_device_rebind(struct acpi_video_bus *video);
286static void acpi_video_device_bind(struct acpi_video_bus *video,
287 struct acpi_video_device *device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static int acpi_video_device_enumerate(struct acpi_video_bus *video);
Yu Luming2f3d0002006-11-11 02:40:34 +0800289static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
290 int level);
291static int acpi_video_device_lcd_get_level_current(
292 struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400293 unsigned long long *level);
Len Brown4be44fc2005-08-05 00:44:28 -0400294static int acpi_video_get_next_level(struct acpi_video_device *device,
295 u32 level_current, u32 event);
296static void acpi_video_switch_brightness(struct acpi_video_device *device,
297 int event);
Luming Yu23b0f012007-05-09 21:07:05 +0800298static int acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400299 unsigned long long *state);
Luming Yu23b0f012007-05-09 21:07:05 +0800300static int acpi_video_output_get(struct output_device *od);
301static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Yu Luming2f3d0002006-11-11 02:40:34 +0800303/*backlight device sysfs support*/
304static int acpi_video_get_brightness(struct backlight_device *bd)
305{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400306 unsigned long long cur_level;
Matthew Garrett38531e62007-12-26 02:03:26 +0000307 int i;
Yu Luming2f3d0002006-11-11 02:40:34 +0800308 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100309 (struct acpi_video_device *)bl_get_data(bd);
Yu Luming2f3d0002006-11-11 02:40:34 +0800310 acpi_video_device_lcd_get_level_current(vd, &cur_level);
Matthew Garrett38531e62007-12-26 02:03:26 +0000311 for (i = 2; i < vd->brightness->count; i++) {
312 if (vd->brightness->levels[i] == cur_level)
313 /* The first two entries are special - see page 575
314 of the ACPI spec 3.0 */
315 return i-2;
316 }
317 return 0;
Yu Luming2f3d0002006-11-11 02:40:34 +0800318}
319
320static int acpi_video_set_brightness(struct backlight_device *bd)
321{
Matthew Garrett38531e62007-12-26 02:03:26 +0000322 int request_level = bd->props.brightness+2;
Yu Luming2f3d0002006-11-11 02:40:34 +0800323 struct acpi_video_device *vd =
Richard Purdie655bfd72007-07-09 12:17:24 +0100324 (struct acpi_video_device *)bl_get_data(bd);
Matthew Garrett38531e62007-12-26 02:03:26 +0000325 acpi_video_device_lcd_set_level(vd,
326 vd->brightness->levels[request_level]);
Yu Luming2f3d0002006-11-11 02:40:34 +0800327 return 0;
328}
329
Richard Purdie599a52d2007-02-10 23:07:48 +0000330static struct backlight_ops acpi_backlight_ops = {
331 .get_brightness = acpi_video_get_brightness,
332 .update_status = acpi_video_set_brightness,
333};
334
Luming Yu23b0f012007-05-09 21:07:05 +0800335/*video output device sysfs support*/
336static int acpi_video_output_get(struct output_device *od)
337{
Matthew Wilcox27663c52008-10-10 02:22:59 -0400338 unsigned long long state;
Luming Yu23b0f012007-05-09 21:07:05 +0800339 struct acpi_video_device *vd =
tonyj@suse.de60043422007-08-07 22:28:47 -0700340 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800341 acpi_video_device_get_state(vd, &state);
342 return (int)state;
343}
344
345static int acpi_video_output_set(struct output_device *od)
346{
347 unsigned long state = od->request_state;
348 struct acpi_video_device *vd=
tonyj@suse.de60043422007-08-07 22:28:47 -0700349 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
Luming Yu23b0f012007-05-09 21:07:05 +0800350 return acpi_video_device_set_state(vd, state);
351}
352
353static struct output_properties acpi_output_properties = {
354 .set_state = acpi_video_output_set,
355 .get_status = acpi_video_output_get,
356};
Zhang Rui702ed512008-01-17 15:51:22 +0800357
358
359/* thermal cooling device callbacks */
360static int video_get_max_state(struct thermal_cooling_device *cdev, char *buf)
361{
362 struct acpi_device *device = cdev->devdata;
363 struct acpi_video_device *video = acpi_driver_data(device);
364
365 return sprintf(buf, "%d\n", video->brightness->count - 3);
366}
367
368static int video_get_cur_state(struct thermal_cooling_device *cdev, char *buf)
369{
370 struct acpi_device *device = cdev->devdata;
371 struct acpi_video_device *video = acpi_driver_data(device);
Matthew Wilcox27663c52008-10-10 02:22:59 -0400372 unsigned long long level;
Zhang Rui702ed512008-01-17 15:51:22 +0800373 int state;
374
375 acpi_video_device_lcd_get_level_current(video, &level);
376 for (state = 2; state < video->brightness->count; state++)
377 if (level == video->brightness->levels[state])
378 return sprintf(buf, "%d\n",
379 video->brightness->count - state - 1);
380
381 return -EINVAL;
382}
383
384static int
385video_set_cur_state(struct thermal_cooling_device *cdev, unsigned int state)
386{
387 struct acpi_device *device = cdev->devdata;
388 struct acpi_video_device *video = acpi_driver_data(device);
389 int level;
390
391 if ( state >= video->brightness->count - 2)
392 return -EINVAL;
393
394 state = video->brightness->count - state;
395 level = video->brightness->levels[state -1];
396 return acpi_video_device_lcd_set_level(video, level);
397}
398
399static struct thermal_cooling_device_ops video_cooling_ops = {
400 .get_max_state = video_get_max_state,
401 .get_cur_state = video_get_cur_state,
402 .set_cur_state = video_set_cur_state,
403};
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405/* --------------------------------------------------------------------------
406 Video Management
407 -------------------------------------------------------------------------- */
408
409/* device */
410
411static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400412acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Len Brown4be44fc2005-08-05 00:44:28 -0400414 int status;
Patrick Mochel90130262006-05-19 16:54:48 -0400415
416 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Patrick Mocheld550d982006-06-27 00:41:40 -0400418 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421static int
Len Brown4be44fc2005-08-05 00:44:28 -0400422acpi_video_device_get_state(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400423 unsigned long long *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Len Brown4be44fc2005-08-05 00:44:28 -0400425 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Patrick Mochel90130262006-05-19 16:54:48 -0400427 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Patrick Mocheld550d982006-06-27 00:41:40 -0400429 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432static int
Len Brown4be44fc2005-08-05 00:44:28 -0400433acpi_video_device_set_state(struct acpi_video_device *device, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Len Brown4be44fc2005-08-05 00:44:28 -0400435 int status;
436 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
437 struct acpi_object_list args = { 1, &arg0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -0400438 unsigned long long ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 arg0.integer.value = state;
Patrick Mochel90130262006-05-19 16:54:48 -0400442 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Patrick Mocheld550d982006-06-27 00:41:40 -0400444 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447static int
Len Brown4be44fc2005-08-05 00:44:28 -0400448acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
449 union acpi_object **levels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Len Brown4be44fc2005-08-05 00:44:28 -0400451 int status;
452 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
453 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 *levels = NULL;
457
Patrick Mochel90130262006-05-19 16:54:48 -0400458 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400460 return status;
Len Brown4be44fc2005-08-05 00:44:28 -0400461 obj = (union acpi_object *)buffer.pointer;
Adrian Bunk6665bda2006-03-11 10:12:00 -0500462 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
Len Brown64684632006-06-26 23:41:38 -0400463 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 status = -EFAULT;
465 goto err;
466 }
467
468 *levels = obj;
469
Patrick Mocheld550d982006-06-27 00:41:40 -0400470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Len Brown4be44fc2005-08-05 00:44:28 -0400472 err:
Jesper Juhl6044ec82005-11-07 01:01:32 -0800473 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Patrick Mocheld550d982006-06-27 00:41:40 -0400475 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478static int
Len Brown4be44fc2005-08-05 00:44:28 -0400479acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400481 int status = AE_OK;
Len Brown4be44fc2005-08-05 00:44:28 -0400482 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
483 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 arg0.integer.value = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400488 if (device->cap._BCM)
489 status = acpi_evaluate_object(device->dev->handle, "_BCM",
490 &args, NULL);
491 device->brightness->curr = level;
Patrick Mocheld550d982006-06-27 00:41:40 -0400492 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
495static int
Len Brown4be44fc2005-08-05 00:44:28 -0400496acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400497 unsigned long long *level)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Alexey Starikovskiy4500ca82007-09-03 16:29:58 +0400499 if (device->cap._BQC)
500 return acpi_evaluate_integer(device->dev->handle, "_BQC", NULL,
501 level);
502 *level = device->brightness->curr;
503 return AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506static int
Len Brown4be44fc2005-08-05 00:44:28 -0400507acpi_video_device_EDID(struct acpi_video_device *device,
508 union acpi_object **edid, ssize_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Len Brown4be44fc2005-08-05 00:44:28 -0400510 int status;
511 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
512 union acpi_object *obj;
513 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
514 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 *edid = NULL;
518
519 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400520 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (length == 128)
522 arg0.integer.value = 1;
523 else if (length == 256)
524 arg0.integer.value = 2;
525 else
Patrick Mocheld550d982006-06-27 00:41:40 -0400526 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Patrick Mochel90130262006-05-19 16:54:48 -0400528 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400530 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200532 obj = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 if (obj && obj->type == ACPI_TYPE_BUFFER)
535 *edid = obj;
536 else {
Len Brown64684632006-06-26 23:41:38 -0400537 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 status = -EFAULT;
539 kfree(obj);
540 }
541
Patrick Mocheld550d982006-06-27 00:41:40 -0400542 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545/* bus */
546
547static int
Len Brown4be44fc2005-08-05 00:44:28 -0400548acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Len Brown4be44fc2005-08-05 00:44:28 -0400550 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400551 unsigned long long tmp;
Len Brown4be44fc2005-08-05 00:44:28 -0400552 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
553 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 arg0.integer.value = option;
557
Patrick Mochel90130262006-05-19 16:54:48 -0400558 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400560 status = tmp ? (-EINVAL) : (AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Patrick Mocheld550d982006-06-27 00:41:40 -0400562 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565static int
Matthew Wilcox27663c52008-10-10 02:22:59 -0400566acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 int status;
569
Patrick Mochel90130262006-05-19 16:54:48 -0400570 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Patrick Mocheld550d982006-06-27 00:41:40 -0400572 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
575static int
Len Brown4be44fc2005-08-05 00:44:28 -0400576acpi_video_bus_POST_options(struct acpi_video_bus *video,
Matthew Wilcox27663c52008-10-10 02:22:59 -0400577 unsigned long long *options)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Len Brown4be44fc2005-08-05 00:44:28 -0400579 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Patrick Mochel90130262006-05-19 16:54:48 -0400581 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 *options &= 3;
583
Patrick Mocheld550d982006-06-27 00:41:40 -0400584 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
587/*
588 * Arg:
589 * video : video bus device pointer
590 * bios_flag :
591 * 0. The system BIOS should NOT automatically switch(toggle)
592 * the active display output.
593 * 1. The system BIOS should automatically switch (toggle) the
Julius Volz98fb8fe2007-02-20 16:38:40 +0100594 * active display output. No switch event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * 2. The _DGS value should be locked.
596 * 3. The system BIOS should not automatically switch (toggle) the
597 * active display output, but instead generate the display switch
598 * event notify code.
599 * lcd_flag :
600 * 0. The system BIOS should automatically control the brightness level
Julius Volz98fb8fe2007-02-20 16:38:40 +0100601 * of the LCD when the power changes from AC to DC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * 1. The system BIOS should NOT automatically control the brightness
Julius Volz98fb8fe2007-02-20 16:38:40 +0100603 * level of the LCD when the power changes from AC to DC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 * Return Value:
605 * -1 wrong arg.
606 */
607
608static int
Len Brown4be44fc2005-08-05 00:44:28 -0400609acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Len Brown4be44fc2005-08-05 00:44:28 -0400611 acpi_integer status = 0;
612 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
613 struct acpi_object_list args = { 1, &arg0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Len Brown4be44fc2005-08-05 00:44:28 -0400616 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 status = -1;
618 goto Failed;
619 }
620 arg0.integer.value = (lcd_flag << 2) | bios_flag;
621 video->dos_setting = arg0.integer.value;
Patrick Mochel90130262006-05-19 16:54:48 -0400622 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Len Brown4be44fc2005-08-05 00:44:28 -0400624 Failed:
Patrick Mocheld550d982006-06-27 00:41:40 -0400625 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
628/*
629 * Arg:
630 * device : video output device (LCD, CRT, ..)
631 *
632 * Return Value:
Julia Jomantaite469778c2008-06-23 22:50:42 +0100633 * Maximum brightness level
634 *
635 * Allocate and initialize device->brightness.
636 */
637
638static int
639acpi_video_init_brightness(struct acpi_video_device *device)
640{
641 union acpi_object *obj = NULL;
642 int i, max_level = 0, count = 0;
643 union acpi_object *o;
644 struct acpi_video_device_brightness *br = NULL;
645
646 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
647 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
648 "LCD brightness level\n"));
649 goto out;
650 }
651
652 if (obj->package.count < 2)
653 goto out;
654
655 br = kzalloc(sizeof(*br), GFP_KERNEL);
656 if (!br) {
657 printk(KERN_ERR "can't allocate memory\n");
658 goto out;
659 }
660
661 br->levels = kmalloc(obj->package.count * sizeof *(br->levels),
662 GFP_KERNEL);
663 if (!br->levels)
664 goto out_free;
665
666 for (i = 0; i < obj->package.count; i++) {
667 o = (union acpi_object *)&obj->package.elements[i];
668 if (o->type != ACPI_TYPE_INTEGER) {
669 printk(KERN_ERR PREFIX "Invalid data\n");
670 continue;
671 }
672 br->levels[count] = (u32) o->integer.value;
673
674 if (br->levels[count] > max_level)
675 max_level = br->levels[count];
676 count++;
677 }
678
679 if (count < 2)
680 goto out_free_levels;
681
682 br->count = count;
683 device->brightness = br;
684 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "found %d brightness levels\n", count));
685 kfree(obj);
686 return max_level;
687
688out_free_levels:
689 kfree(br->levels);
690out_free:
691 kfree(br);
692out:
693 device->brightness = NULL;
694 kfree(obj);
695 return 0;
696}
697
698/*
699 * Arg:
700 * device : video output device (LCD, CRT, ..)
701 *
702 * Return Value:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * None
704 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100705 * Find out all required AML methods defined under the output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 * device.
707 */
708
Len Brown4be44fc2005-08-05 00:44:28 -0400709static void acpi_video_device_find_cap(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 acpi_handle h_dummy1;
Yu Luming2f3d0002006-11-11 02:40:34 +0800712 u32 max_level = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
William Lee Irwin III98934de2007-12-12 03:56:55 -0800715 memset(&device->cap, 0, sizeof(device->cap));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Patrick Mochel90130262006-05-19 16:54:48 -0400717 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 device->cap._ADR = 1;
719 }
Patrick Mochel90130262006-05-19 16:54:48 -0400720 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400721 device->cap._BCL = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Patrick Mochel90130262006-05-19 16:54:48 -0400723 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400724 device->cap._BCM = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
Yu Luming2f3d0002006-11-11 02:40:34 +0800726 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
727 device->cap._BQC = 1;
Patrick Mochel90130262006-05-19 16:54:48 -0400728 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
Len Brown4be44fc2005-08-05 00:44:28 -0400729 device->cap._DDC = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
Patrick Mochel90130262006-05-19 16:54:48 -0400731 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 device->cap._DCS = 1;
733 }
Patrick Mochel90130262006-05-19 16:54:48 -0400734 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 device->cap._DGS = 1;
736 }
Patrick Mochel90130262006-05-19 16:54:48 -0400737 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 device->cap._DSS = 1;
739 }
740
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200741 if (acpi_video_backlight_support())
742 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 }
Thomas Renningerc3d6de62008-08-01 17:37:55 +0200788
789 if (acpi_video_display_switch_support()) {
790
791 if (device->cap._DCS && device->cap._DSS) {
792 static int count;
793 char *name;
794 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
795 if (!name)
796 return;
797 sprintf(name, "acpi_video%d", count++);
798 device->output_dev = video_output_register(name,
799 NULL, device, &acpi_output_properties);
800 kfree(name);
801 }
Luming Yu23b0f012007-05-09 21:07:05 +0800802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805/*
806 * Arg:
807 * device : video output device (VGA)
808 *
809 * Return Value:
810 * None
811 *
Julius Volz98fb8fe2007-02-20 16:38:40 +0100812 * Find out all required AML methods defined under the video bus device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 */
814
Len Brown4be44fc2005-08-05 00:44:28 -0400815static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Len Brown4be44fc2005-08-05 00:44:28 -0400817 acpi_handle h_dummy1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
William Lee Irwin III98934de2007-12-12 03:56:55 -0800819 memset(&video->cap, 0, sizeof(video->cap));
Patrick Mochel90130262006-05-19 16:54:48 -0400820 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 video->cap._DOS = 1;
822 }
Patrick Mochel90130262006-05-19 16:54:48 -0400823 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 video->cap._DOD = 1;
825 }
Patrick Mochel90130262006-05-19 16:54:48 -0400826 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 video->cap._ROM = 1;
828 }
Patrick Mochel90130262006-05-19 16:54:48 -0400829 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 video->cap._GPD = 1;
831 }
Patrick Mochel90130262006-05-19 16:54:48 -0400832 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 video->cap._SPD = 1;
834 }
Patrick Mochel90130262006-05-19 16:54:48 -0400835 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 video->cap._VPO = 1;
837 }
838}
839
840/*
841 * Check whether the video bus device has required AML method to
842 * support the desired features
843 */
844
Len Brown4be44fc2005-08-05 00:44:28 -0400845static int acpi_video_bus_check(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Len Brown4be44fc2005-08-05 00:44:28 -0400847 acpi_status status = -ENOENT;
Thomas Renninger22c13f92008-08-01 17:37:54 +0200848 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -0400851 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Thomas Renninger22c13f92008-08-01 17:37:54 +0200853 dev = acpi_get_physical_pci_device(video->device->handle);
854 if (!dev)
855 return -ENODEV;
856 put_device(dev);
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 /* Since there is no HID, CID and so on for VGA driver, we have
859 * to check well known required nodes.
860 */
861
Julius Volz98fb8fe2007-02-20 16:38:40 +0100862 /* Does this device support video switching? */
Len Brown4be44fc2005-08-05 00:44:28 -0400863 if (video->cap._DOS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 video->flags.multihead = 1;
865 status = 0;
866 }
867
Julius Volz98fb8fe2007-02-20 16:38:40 +0100868 /* Does this device support retrieving a video ROM? */
Len Brown4be44fc2005-08-05 00:44:28 -0400869 if (video->cap._ROM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 video->flags.rom = 1;
871 status = 0;
872 }
873
Julius Volz98fb8fe2007-02-20 16:38:40 +0100874 /* Does this device support configuring which video device to POST? */
Len Brown4be44fc2005-08-05 00:44:28 -0400875 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 video->flags.post = 1;
877 status = 0;
878 }
879
Patrick Mocheld550d982006-06-27 00:41:40 -0400880 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883/* --------------------------------------------------------------------------
884 FS Interface (/proc)
885 -------------------------------------------------------------------------- */
886
Len Brown4be44fc2005-08-05 00:44:28 -0400887static struct proc_dir_entry *acpi_video_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889/* video devices */
890
Len Brown4be44fc2005-08-05 00:44:28 -0400891static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200893 struct acpi_video_device *dev = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if (!dev)
897 goto end;
898
899 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
900 seq_printf(seq, "type: ");
901 if (dev->flags.crt)
902 seq_printf(seq, "CRT\n");
903 else if (dev->flags.lcd)
904 seq_printf(seq, "LCD\n");
905 else if (dev->flags.tvout)
906 seq_printf(seq, "TVOUT\n");
Rui Zhang82cae992007-01-03 23:40:53 -0500907 else if (dev->flags.dvi)
908 seq_printf(seq, "DVI\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 else
910 seq_printf(seq, "UNKNOWN\n");
911
Len Brown4be44fc2005-08-05 00:44:28 -0400912 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Len Brown4be44fc2005-08-05 00:44:28 -0400914 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
918static int
Len Brown4be44fc2005-08-05 00:44:28 -0400919acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 return single_open(file, acpi_video_device_info_seq_show,
922 PDE(inode)->data);
923}
924
Len Brown4be44fc2005-08-05 00:44:28 -0400925static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Len Brown4be44fc2005-08-05 00:44:28 -0400927 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200928 struct acpi_video_device *dev = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400929 unsigned long long state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 if (!dev)
933 goto end;
934
935 status = acpi_video_device_get_state(dev, &state);
936 seq_printf(seq, "state: ");
937 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -0400938 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 else
940 seq_printf(seq, "<not supported>\n");
941
942 status = acpi_video_device_query(dev, &state);
943 seq_printf(seq, "query: ");
944 if (ACPI_SUCCESS(status))
Matthew Wilcox27663c52008-10-10 02:22:59 -0400945 seq_printf(seq, "0x%02llx\n", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 else
947 seq_printf(seq, "<not supported>\n");
948
Len Brown4be44fc2005-08-05 00:44:28 -0400949 end:
Patrick Mocheld550d982006-06-27 00:41:40 -0400950 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953static int
Len Brown4be44fc2005-08-05 00:44:28 -0400954acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
956 return single_open(file, acpi_video_device_state_seq_show,
957 PDE(inode)->data);
958}
959
960static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -0400961acpi_video_device_write_state(struct file *file,
962 const char __user * buffer,
963 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Len Brown4be44fc2005-08-05 00:44:28 -0400965 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200966 struct seq_file *m = file->private_data;
967 struct acpi_video_device *dev = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400968 char str[12] = { 0 };
969 u32 state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 if (!dev || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -0400973 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -0400976 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 str[count] = 0;
979 state = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -0400980 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 status = acpi_video_device_set_state(dev, state);
983
984 if (status)
Patrick Mocheld550d982006-06-27 00:41:40 -0400985 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Patrick Mocheld550d982006-06-27 00:41:40 -0400987 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
989
990static int
Len Brown4be44fc2005-08-05 00:44:28 -0400991acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200993 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -0400994 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 if (!dev || !dev->brightness) {
998 seq_printf(seq, "<not supported>\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400999 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001
1002 seq_printf(seq, "levels: ");
1003 for (i = 0; i < dev->brightness->count; i++)
1004 seq_printf(seq, " %d", dev->brightness->levels[i]);
1005 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1006
Patrick Mocheld550d982006-06-27 00:41:40 -04001007 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
1010static int
Len Brown4be44fc2005-08-05 00:44:28 -04001011acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 return single_open(file, acpi_video_device_brightness_seq_show,
1014 PDE(inode)->data);
1015}
1016
1017static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001018acpi_video_device_write_brightness(struct file *file,
1019 const char __user * buffer,
1020 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001022 struct seq_file *m = file->private_data;
1023 struct acpi_video_device *dev = m->private;
Danny Baumannc88c5782007-11-02 13:47:53 +01001024 char str[5] = { 0 };
Len Brown4be44fc2005-08-05 00:44:28 -04001025 unsigned int level = 0;
1026 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Thomas Renninger59d399d2005-11-08 05:27:00 -05001029 if (!dev || !dev->brightness || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001030 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001033 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 str[count] = 0;
1036 level = simple_strtoul(str, NULL, 0);
Len Brown4be44fc2005-08-05 00:44:28 -04001037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (level > 100)
Patrick Mocheld550d982006-06-27 00:41:40 -04001039 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Julius Volz98fb8fe2007-02-20 16:38:40 +01001041 /* validate through the list of available levels */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 for (i = 0; i < dev->brightness->count; i++)
1043 if (level == dev->brightness->levels[i]) {
Len Brown4be44fc2005-08-05 00:44:28 -04001044 if (ACPI_SUCCESS
1045 (acpi_video_device_lcd_set_level(dev, level)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 dev->brightness->curr = level;
1047 break;
1048 }
1049
Patrick Mocheld550d982006-06-27 00:41:40 -04001050 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
Len Brown4be44fc2005-08-05 00:44:28 -04001053static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001055 struct acpi_video_device *dev = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001056 int status;
1057 int i;
1058 union acpi_object *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 if (!dev)
1062 goto out;
1063
Len Brown4be44fc2005-08-05 00:44:28 -04001064 status = acpi_video_device_EDID(dev, &edid, 128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001066 status = acpi_video_device_EDID(dev, &edid, 256);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
1069 if (ACPI_FAILURE(status)) {
1070 goto out;
1071 }
1072
1073 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1074 for (i = 0; i < edid->buffer.length; i++)
1075 seq_putc(seq, edid->buffer.pointer[i]);
1076 }
1077
Len Brown4be44fc2005-08-05 00:44:28 -04001078 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (!edid)
1080 seq_printf(seq, "<not supported>\n");
1081 else
1082 kfree(edid);
1083
Patrick Mocheld550d982006-06-27 00:41:40 -04001084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
1087static int
Len Brown4be44fc2005-08-05 00:44:28 -04001088acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
1090 return single_open(file, acpi_video_device_EDID_seq_show,
1091 PDE(inode)->data);
1092}
1093
Len Brown4be44fc2005-08-05 00:44:28 -04001094static int acpi_video_device_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001096 struct proc_dir_entry *entry, *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 struct acpi_video_device *vid_dev;
1098
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001099 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (!vid_dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001101 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001103 device_dir = proc_mkdir(acpi_device_bid(device),
1104 vid_dev->video->dir);
1105 if (!device_dir)
1106 return -ENOMEM;
1107
1108 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001111 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001112 &acpi_video_device_info_fops, acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001114 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
1116 /* 'state' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001117 acpi_video_device_state_fops.write = acpi_video_device_write_state;
1118 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001119 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001120 &acpi_video_device_state_fops,
1121 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001123 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 /* 'brightness' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001126 acpi_video_device_brightness_fops.write =
1127 acpi_video_device_write_brightness;
1128 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001129 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001130 &acpi_video_device_brightness_fops,
1131 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001133 goto err_remove_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 /* 'EDID' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001136 entry = proc_create_data("EDID", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001137 &acpi_video_device_EDID_fops,
1138 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001140 goto err_remove_brightness;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001142 acpi_device_dir(device) = device_dir;
1143
Patrick Mocheld550d982006-06-27 00:41:40 -04001144 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001145
1146 err_remove_brightness:
1147 remove_proc_entry("brightness", device_dir);
1148 err_remove_state:
1149 remove_proc_entry("state", device_dir);
1150 err_remove_info:
1151 remove_proc_entry("info", device_dir);
1152 err_remove_dir:
1153 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1154 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
1156
Len Brown4be44fc2005-08-05 00:44:28 -04001157static int acpi_video_device_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 struct acpi_video_device *vid_dev;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001160 struct proc_dir_entry *device_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001162 vid_dev = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04001164 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001166 device_dir = acpi_device_dir(device);
1167 if (device_dir) {
1168 remove_proc_entry("info", device_dir);
1169 remove_proc_entry("state", device_dir);
1170 remove_proc_entry("brightness", device_dir);
1171 remove_proc_entry("EDID", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001172 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 acpi_device_dir(device) = NULL;
1174 }
1175
Patrick Mocheld550d982006-06-27 00:41:40 -04001176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179/* video bus */
Len Brown4be44fc2005-08-05 00:44:28 -04001180static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001182 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if (!video)
1186 goto end;
1187
1188 seq_printf(seq, "Switching heads: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001189 video->flags.multihead ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 seq_printf(seq, "Video ROM: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001191 video->flags.rom ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 seq_printf(seq, "Device to be POSTed on boot: %s\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001193 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Len Brown4be44fc2005-08-05 00:44:28 -04001195 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001196 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Len Brown4be44fc2005-08-05 00:44:28 -04001199static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Len Brown4be44fc2005-08-05 00:44:28 -04001201 return single_open(file, acpi_video_bus_info_seq_show,
1202 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203}
1204
Len Brown4be44fc2005-08-05 00:44:28 -04001205static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001207 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 if (!video)
1211 goto end;
1212
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001213 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 seq_printf(seq, "<TODO>\n");
1215
Len Brown4be44fc2005-08-05 00:44:28 -04001216 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Len Brown4be44fc2005-08-05 00:44:28 -04001220static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1223}
1224
Len Brown4be44fc2005-08-05 00:44:28 -04001225static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001227 struct acpi_video_bus *video = seq->private;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001228 unsigned long long options;
Len Brown4be44fc2005-08-05 00:44:28 -04001229 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 if (!video)
1233 goto end;
1234
1235 status = acpi_video_bus_POST_options(video, &options);
1236 if (ACPI_SUCCESS(status)) {
1237 if (!(options & 1)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001238 printk(KERN_WARNING PREFIX
1239 "The motherboard VGA device is not listed as a possible POST device.\n");
1240 printk(KERN_WARNING PREFIX
Julius Volz98fb8fe2007-02-20 16:38:40 +01001241 "This indicates a BIOS bug. Please contact the manufacturer.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
Matthew Wilcox27663c52008-10-10 02:22:59 -04001243 printk("%llx\n", options);
Julius Volz98fb8fe2007-02-20 16:38:40 +01001244 seq_printf(seq, "can POST: <integrated video>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (options & 2)
1246 seq_printf(seq, " <PCI video>");
1247 if (options & 4)
1248 seq_printf(seq, " <AGP video>");
1249 seq_putc(seq, '\n');
1250 } else
1251 seq_printf(seq, "<not supported>\n");
Len Brown4be44fc2005-08-05 00:44:28 -04001252 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
1256static int
Len Brown4be44fc2005-08-05 00:44:28 -04001257acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Len Brown4be44fc2005-08-05 00:44:28 -04001259 return single_open(file, acpi_video_bus_POST_info_seq_show,
1260 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
1262
Len Brown4be44fc2005-08-05 00:44:28 -04001263static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001265 struct acpi_video_bus *video = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001266 int status;
Matthew Wilcox27663c52008-10-10 02:22:59 -04001267 unsigned long long id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 if (!video)
1271 goto end;
1272
Len Brown4be44fc2005-08-05 00:44:28 -04001273 status = acpi_video_bus_get_POST(video, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (!ACPI_SUCCESS(status)) {
1275 seq_printf(seq, "<not supported>\n");
1276 goto end;
1277 }
Julius Volz98fb8fe2007-02-20 16:38:40 +01001278 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Len Brown4be44fc2005-08-05 00:44:28 -04001280 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
Len Brown4be44fc2005-08-05 00:44:28 -04001284static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001286 struct acpi_video_bus *video = seq->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Len Brown4be44fc2005-08-05 00:44:28 -04001289 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Patrick Mocheld550d982006-06-27 00:41:40 -04001291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Len Brown4be44fc2005-08-05 00:44:28 -04001294static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Len Brown4be44fc2005-08-05 00:44:28 -04001296 return single_open(file, acpi_video_bus_POST_seq_show,
1297 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
Len Brown4be44fc2005-08-05 00:44:28 -04001300static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1303}
1304
1305static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001306acpi_video_bus_write_POST(struct file *file,
1307 const char __user * buffer,
1308 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Len Brown4be44fc2005-08-05 00:44:28 -04001310 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001311 struct seq_file *m = file->private_data;
1312 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001313 char str[12] = { 0 };
Matthew Wilcox27663c52008-10-10 02:22:59 -04001314 unsigned long long opt, options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001318 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 status = acpi_video_bus_POST_options(video, &options);
1321 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001322 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001325 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 str[count] = 0;
1328 opt = strtoul(str, NULL, 0);
1329 if (opt > 3)
Patrick Mocheld550d982006-06-27 00:41:40 -04001330 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Julius Volz98fb8fe2007-02-20 16:38:40 +01001332 /* just in case an OEM 'forgot' the motherboard... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 options |= 1;
1334
1335 if (options & (1ul << opt)) {
Len Brown4be44fc2005-08-05 00:44:28 -04001336 status = acpi_video_bus_set_POST(video, opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001338 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 }
1341
Patrick Mocheld550d982006-06-27 00:41:40 -04001342 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
1345static ssize_t
Len Brown4be44fc2005-08-05 00:44:28 -04001346acpi_video_bus_write_DOS(struct file *file,
1347 const char __user * buffer,
1348 size_t count, loff_t * data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Len Brown4be44fc2005-08-05 00:44:28 -04001350 int status;
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001351 struct seq_file *m = file->private_data;
1352 struct acpi_video_bus *video = m->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001353 char str[12] = { 0 };
1354 unsigned long opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (!video || count + 1 > sizeof str)
Patrick Mocheld550d982006-06-27 00:41:40 -04001358 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 if (copy_from_user(str, buffer, count))
Patrick Mocheld550d982006-06-27 00:41:40 -04001361 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 str[count] = 0;
1364 opt = strtoul(str, NULL, 0);
1365 if (opt > 7)
Patrick Mocheld550d982006-06-27 00:41:40 -04001366 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Len Brown4be44fc2005-08-05 00:44:28 -04001368 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 if (!ACPI_SUCCESS(status))
Patrick Mocheld550d982006-06-27 00:41:40 -04001371 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Patrick Mocheld550d982006-06-27 00:41:40 -04001373 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Len Brown4be44fc2005-08-05 00:44:28 -04001376static int acpi_video_bus_add_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001378 struct acpi_video_bus *video = acpi_driver_data(device);
1379 struct proc_dir_entry *device_dir;
1380 struct proc_dir_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001382 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1383 if (!device_dir)
1384 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001386 device_dir->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 /* 'info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001389 entry = proc_create_data("info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001390 &acpi_video_bus_info_fops,
1391 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001393 goto err_remove_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 /* 'ROM' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001396 entry = proc_create_data("ROM", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001397 &acpi_video_bus_ROM_fops,
1398 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001400 goto err_remove_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 /* 'POST_info' [R] */
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001403 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001404 &acpi_video_bus_POST_info_fops,
1405 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001407 goto err_remove_rom;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 /* 'POST' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001410 acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001411 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001412 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001413 &acpi_video_bus_POST_fops,
1414 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001416 goto err_remove_post_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 /* 'DOS' [R/W] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001419 acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
Linus Torvalds08acd4f82008-04-30 11:52:52 -07001420 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
Alexey Dobriyane0066c4e2008-05-01 04:10:02 +04001421 device_dir,
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001422 &acpi_video_bus_DOS_fops,
1423 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (!entry)
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001425 goto err_remove_post;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001427 video->dir = acpi_device_dir(device) = device_dir;
Patrick Mocheld550d982006-06-27 00:41:40 -04001428 return 0;
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001429
1430 err_remove_post:
1431 remove_proc_entry("POST", device_dir);
1432 err_remove_post_info:
1433 remove_proc_entry("POST_info", device_dir);
1434 err_remove_rom:
1435 remove_proc_entry("ROM", device_dir);
1436 err_remove_info:
1437 remove_proc_entry("info", device_dir);
1438 err_remove_dir:
1439 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1440 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
Len Brown4be44fc2005-08-05 00:44:28 -04001443static int acpi_video_bus_remove_fs(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001445 struct proc_dir_entry *device_dir = acpi_device_dir(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Dmitry Torokhov251cb0b2007-11-05 11:43:34 -05001447 if (device_dir) {
1448 remove_proc_entry("info", device_dir);
1449 remove_proc_entry("ROM", device_dir);
1450 remove_proc_entry("POST_info", device_dir);
1451 remove_proc_entry("POST", device_dir);
1452 remove_proc_entry("DOS", device_dir);
Len Brown4be44fc2005-08-05 00:44:28 -04001453 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 acpi_device_dir(device) = NULL;
1455 }
1456
Patrick Mocheld550d982006-06-27 00:41:40 -04001457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
1460/* --------------------------------------------------------------------------
1461 Driver Interface
1462 -------------------------------------------------------------------------- */
1463
1464/* device interface */
Rui Zhang82cae992007-01-03 23:40:53 -05001465static struct acpi_video_device_attrib*
1466acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1467{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001468 struct acpi_video_enumerated_device *ids;
1469 int i;
Rui Zhang82cae992007-01-03 23:40:53 -05001470
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001471 for (i = 0; i < video->attached_count; i++) {
1472 ids = &video->attached_array[i];
1473 if ((ids->value.int_val & 0xffff) == device_id)
1474 return &ids->value.attrib;
1475 }
1476
Rui Zhang82cae992007-01-03 23:40:53 -05001477 return NULL;
1478}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480static int
Len Brown4be44fc2005-08-05 00:44:28 -04001481acpi_video_bus_get_one_device(struct acpi_device *device,
1482 struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001484 unsigned long long device_id;
Yu, Luming973bf492006-04-27 05:25:00 -04001485 int status;
Len Brown4be44fc2005-08-05 00:44:28 -04001486 struct acpi_video_device *data;
Rui Zhang82cae992007-01-03 23:40:53 -05001487 struct acpi_video_device_attrib* attribute;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (!device || !video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Len Brown4be44fc2005-08-05 00:44:28 -04001492 status =
1493 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (ACPI_SUCCESS(status)) {
1495
Burman Yan36bcbec2006-12-19 12:56:11 -08001496 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (!data)
Patrick Mocheld550d982006-06-27 00:41:40 -04001498 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1501 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001502 device->driver_data = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 data->device_id = device_id;
1505 data->video = video;
1506 data->dev = device;
1507
Rui Zhang82cae992007-01-03 23:40:53 -05001508 attribute = acpi_video_get_device_attr(video, device_id);
1509
1510 if((attribute != NULL) && attribute->device_id_scheme) {
1511 switch (attribute->display_type) {
1512 case ACPI_VIDEO_DISPLAY_CRT:
1513 data->flags.crt = 1;
1514 break;
1515 case ACPI_VIDEO_DISPLAY_TV:
1516 data->flags.tvout = 1;
1517 break;
1518 case ACPI_VIDEO_DISPLAY_DVI:
1519 data->flags.dvi = 1;
1520 break;
1521 case ACPI_VIDEO_DISPLAY_LCD:
1522 data->flags.lcd = 1;
1523 break;
1524 default:
1525 data->flags.unknown = 1;
1526 break;
1527 }
1528 if(attribute->bios_can_detect)
1529 data->flags.bios = 1;
1530 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 data->flags.unknown = 1;
Len Brown4be44fc2005-08-05 00:44:28 -04001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 acpi_video_device_bind(video, data);
1534 acpi_video_device_find_cap(data);
1535
Patrick Mochel90130262006-05-19 16:54:48 -04001536 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001537 ACPI_DEVICE_NOTIFY,
1538 acpi_video_device_notify,
1539 data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001541 printk(KERN_ERR PREFIX
1542 "Error installing notify handler\n");
Yu, Luming973bf492006-04-27 05:25:00 -04001543 if(data->brightness)
1544 kfree(data->brightness->levels);
1545 kfree(data->brightness);
1546 kfree(data);
1547 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001550 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 list_add_tail(&data->entry, &video->video_device_list);
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001552 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 acpi_video_device_add_fs(device);
1555
Patrick Mocheld550d982006-06-27 00:41:40 -04001556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558
Patrick Mocheld550d982006-06-27 00:41:40 -04001559 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
1562/*
1563 * Arg:
1564 * video : video bus device
1565 *
1566 * Return:
1567 * none
1568 *
1569 * Enumerate the video device list of the video bus,
1570 * bind the ids with the corresponding video devices
1571 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001572 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Len Brown4be44fc2005-08-05 00:44:28 -04001574static void acpi_video_device_rebind(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001576 struct acpi_video_device *dev;
1577
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001578 mutex_lock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001579
1580 list_for_each_entry(dev, &video->video_device_list, entry)
Len Brown4be44fc2005-08-05 00:44:28 -04001581 acpi_video_device_bind(video, dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001582
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001583 mutex_unlock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
1586/*
1587 * Arg:
1588 * video : video bus device
1589 * device : video output device under the video
1590 * bus
1591 *
1592 * Return:
1593 * none
1594 *
1595 * Bind the ids with the corresponding video devices
1596 * under the video bus.
Len Brown4be44fc2005-08-05 00:44:28 -04001597 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599static void
Len Brown4be44fc2005-08-05 00:44:28 -04001600acpi_video_device_bind(struct acpi_video_bus *video,
1601 struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001603 struct acpi_video_enumerated_device *ids;
Len Brown4be44fc2005-08-05 00:44:28 -04001604 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001606 for (i = 0; i < video->attached_count; i++) {
1607 ids = &video->attached_array[i];
1608 if (device->device_id == (ids->value.int_val & 0xffff)) {
1609 ids->bind_info = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1611 }
1612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
1615/*
1616 * Arg:
1617 * video : video bus device
1618 *
1619 * Return:
1620 * < 0 : error
1621 *
1622 * Call _DOD to enumerate all devices attached to display adapter
1623 *
Len Brown4be44fc2005-08-05 00:44:28 -04001624 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1627{
Len Brown4be44fc2005-08-05 00:44:28 -04001628 int status;
1629 int count;
1630 int i;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001631 struct acpi_video_enumerated_device *active_list;
Len Brown4be44fc2005-08-05 00:44:28 -04001632 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1633 union acpi_object *dod = NULL;
1634 union acpi_object *obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Patrick Mochel90130262006-05-19 16:54:48 -04001636 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (!ACPI_SUCCESS(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001638 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001639 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
1641
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001642 dod = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001644 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 status = -EFAULT;
1646 goto out;
1647 }
1648
1649 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001650 dod->package.count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001652 active_list = kcalloc(1 + dod->package.count,
1653 sizeof(struct acpi_video_enumerated_device),
1654 GFP_KERNEL);
1655 if (!active_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 status = -ENOMEM;
1657 goto out;
1658 }
1659
1660 count = 0;
1661 for (i = 0; i < dod->package.count; i++) {
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001662 obj = &dod->package.elements[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 if (obj->type != ACPI_TYPE_INTEGER) {
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001665 printk(KERN_ERR PREFIX
1666 "Invalid _DOD data in element %d\n", i);
1667 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001669
1670 active_list[count].value.int_val = obj->integer.value;
1671 active_list[count].bind_info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001672 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1673 (int)obj->integer.value));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 count++;
1675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Jesper Juhl6044ec82005-11-07 01:01:32 -08001677 kfree(video->attached_array);
Len Brown4be44fc2005-08-05 00:44:28 -04001678
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001679 video->attached_array = active_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 video->attached_count = count;
Dmitry Torokhov78eed022007-11-05 11:43:33 -05001681
1682 out:
Len Brown02438d82006-06-30 03:19:10 -04001683 kfree(buffer.pointer);
Patrick Mocheld550d982006-06-27 00:41:40 -04001684 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
Len Brown4be44fc2005-08-05 00:44:28 -04001687static int
1688acpi_video_get_next_level(struct acpi_video_device *device,
1689 u32 level_current, u32 event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001691 int min, max, min_above, max_below, i, l, delta = 255;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001692 max = max_below = 0;
1693 min = min_above = 255;
Alexey Starikovskiy63f0edf2007-09-03 16:30:08 +04001694 /* Find closest level to level_current */
1695 for (i = 0; i < device->brightness->count; i++) {
1696 l = device->brightness->levels[i];
1697 if (abs(l - level_current) < abs(delta)) {
1698 delta = l - level_current;
1699 if (!delta)
1700 break;
1701 }
1702 }
1703 /* Ajust level_current to closest available level */
1704 level_current += delta;
Thomas Tuttlef4715182006-12-19 12:56:14 -08001705 for (i = 0; i < device->brightness->count; i++) {
1706 l = device->brightness->levels[i];
1707 if (l < min)
1708 min = l;
1709 if (l > max)
1710 max = l;
1711 if (l < min_above && l > level_current)
1712 min_above = l;
1713 if (l > max_below && l < level_current)
1714 max_below = l;
1715 }
1716
1717 switch (event) {
1718 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1719 return (level_current < max) ? min_above : min;
1720 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1721 return (level_current < max) ? min_above : max;
1722 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1723 return (level_current > min) ? max_below : min;
1724 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1725 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1726 return 0;
1727 default:
1728 return level_current;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730}
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732static void
Len Brown4be44fc2005-08-05 00:44:28 -04001733acpi_video_switch_brightness(struct acpi_video_device *device, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Matthew Wilcox27663c52008-10-10 02:22:59 -04001735 unsigned long long level_current, level_next;
Julia Jomantaite469778c2008-06-23 22:50:42 +01001736 if (!device->brightness)
1737 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 acpi_video_device_lcd_get_level_current(device, &level_current);
1739 level_next = acpi_video_get_next_level(device, level_current, event);
1740 acpi_video_device_lcd_set_level(device, level_next);
1741}
1742
1743static int
Len Brown4be44fc2005-08-05 00:44:28 -04001744acpi_video_bus_get_devices(struct acpi_video_bus *video,
1745 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Len Brown4be44fc2005-08-05 00:44:28 -04001747 int status = 0;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001748 struct acpi_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 acpi_video_device_enumerate(video);
1751
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001752 list_for_each_entry(dev, &device->children, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 status = acpi_video_bus_get_one_device(dev, video);
1755 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08001756 printk(KERN_WARNING PREFIX
1757 "Cant attach device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 continue;
1759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001761 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
Len Brown4be44fc2005-08-05 00:44:28 -04001764static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Karol Kozimor031ec772005-07-30 04:18:00 -04001766 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 struct acpi_video_bus *video;
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 if (!device || !device->video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001771 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 video = device->video;
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 acpi_video_device_remove_fs(device->dev);
1776
Patrick Mochel90130262006-05-19 16:54:48 -04001777 status = acpi_remove_notify_handler(device->dev->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001778 ACPI_DEVICE_NOTIFY,
1779 acpi_video_device_notify);
Richard Purdie599a52d2007-02-10 23:07:48 +00001780 backlight_device_unregister(device->backlight);
Zhang Rui702ed512008-01-17 15:51:22 +08001781 if (device->cdev) {
1782 sysfs_remove_link(&device->dev->dev.kobj,
1783 "thermal_cooling");
1784 sysfs_remove_link(&device->cdev->device.kobj,
1785 "device");
1786 thermal_cooling_device_unregister(device->cdev);
1787 device->cdev = NULL;
1788 }
Luming Yu23b0f012007-05-09 21:07:05 +08001789 video_output_unregister(device->output_dev);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001790
Patrick Mocheld550d982006-06-27 00:41:40 -04001791 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
1793
Len Brown4be44fc2005-08-05 00:44:28 -04001794static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
Len Brown4be44fc2005-08-05 00:44:28 -04001796 int status;
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001797 struct acpi_video_device *dev, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001799 mutex_lock(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001801 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001803 status = acpi_video_bus_put_one_device(dev);
Len Brown4be44fc2005-08-05 00:44:28 -04001804 if (ACPI_FAILURE(status))
1805 printk(KERN_WARNING PREFIX
1806 "hhuuhhuu bug in acpi video driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001808 if (dev->brightness) {
1809 kfree(dev->brightness->levels);
1810 kfree(dev->brightness);
1811 }
1812 list_del(&dev->entry);
1813 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 }
1815
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05001816 mutex_unlock(&video->device_list_lock);
Dmitry Torokhovff102ea2007-11-05 11:43:31 -05001817
Patrick Mocheld550d982006-06-27 00:41:40 -04001818 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820
1821/* acpi_video interface */
1822
Len Brown4be44fc2005-08-05 00:44:28 -04001823static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Zhang Ruia21101c2007-09-14 11:46:22 +08001825 return acpi_video_bus_DOS(video, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
Len Brown4be44fc2005-08-05 00:44:28 -04001828static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 return acpi_video_bus_DOS(video, 0, 1);
1831}
1832
Len Brown4be44fc2005-08-05 00:44:28 -04001833static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001835 struct acpi_video_bus *video = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001836 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001837 struct input_dev *input;
1838 int keycode;
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001841 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001843 device = video->device;
Luming Yue9dab192007-08-20 18:23:53 +08001844 input = video->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 switch (event) {
Julius Volz98fb8fe2007-02-20 16:38:40 +01001847 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 * most likely via hotkey. */
Len Brown14e04fb2007-08-23 15:20:26 -04001849 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001850 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 break;
1852
Julius Volz98fb8fe2007-02-20 16:38:40 +01001853 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 * connector. */
1855 acpi_video_device_enumerate(video);
1856 acpi_video_device_rebind(video);
Len Brown14e04fb2007-08-23 15:20:26 -04001857 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001858 keycode = KEY_SWITCHVIDEOMODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 break;
1860
Len Brown4be44fc2005-08-05 00:44:28 -04001861 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001862 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001863 keycode = KEY_SWITCHVIDEOMODE;
1864 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001865 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
Len Brown25c87f72007-08-25 01:44:01 -04001866 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001867 keycode = KEY_VIDEO_NEXT;
1868 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001869 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
Len Brown14e04fb2007-08-23 15:20:26 -04001870 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001871 keycode = KEY_VIDEO_PREV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 break;
1873
1874 default:
Luming Yue9dab192007-08-20 18:23:53 +08001875 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001877 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 break;
1879 }
1880
Zhang Rui7761f632008-01-25 14:48:12 +08001881 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001882 input_report_key(input, keycode, 1);
1883 input_sync(input);
1884 input_report_key(input, keycode, 0);
1885 input_sync(input);
1886
Patrick Mocheld550d982006-06-27 00:41:40 -04001887 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
Len Brown4be44fc2005-08-05 00:44:28 -04001890static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001892 struct acpi_video_device *video_device = data;
Len Brown4be44fc2005-08-05 00:44:28 -04001893 struct acpi_device *device = NULL;
Luming Yue9dab192007-08-20 18:23:53 +08001894 struct acpi_video_bus *bus;
1895 struct input_dev *input;
1896 int keycode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (!video_device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001899 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001901 device = video_device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08001902 bus = video_device->video;
1903 input = bus->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 switch (event) {
Len Brown4be44fc2005-08-05 00:44:28 -04001906 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001907 if (brightness_switch_enabled)
1908 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001909 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001910 keycode = KEY_BRIGHTNESS_CYCLE;
1911 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001912 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001913 if (brightness_switch_enabled)
1914 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001915 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001916 keycode = KEY_BRIGHTNESSUP;
1917 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001918 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
Zhang Rui8a681a42008-01-25 14:47:49 +08001919 if (brightness_switch_enabled)
1920 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001921 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001922 keycode = KEY_BRIGHTNESSDOWN;
1923 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001924 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
Zhang Rui8a681a42008-01-25 14:47:49 +08001925 if (brightness_switch_enabled)
1926 acpi_video_switch_brightness(video_device, event);
Len Brown25c87f72007-08-25 01:44:01 -04001927 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001928 keycode = KEY_BRIGHTNESS_ZERO;
1929 break;
Len Brown4be44fc2005-08-05 00:44:28 -04001930 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
Zhang Rui8a681a42008-01-25 14:47:49 +08001931 if (brightness_switch_enabled)
1932 acpi_video_switch_brightness(video_device, event);
Len Brown14e04fb2007-08-23 15:20:26 -04001933 acpi_bus_generate_proc_event(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001934 keycode = KEY_DISPLAY_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 break;
1936 default:
Luming Yue9dab192007-08-20 18:23:53 +08001937 keycode = KEY_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001939 "Unsupported event [0x%x]\n", event));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 break;
1941 }
Luming Yue9dab192007-08-20 18:23:53 +08001942
Zhang Rui7761f632008-01-25 14:48:12 +08001943 acpi_notifier_call_chain(device, event, 0);
Luming Yue9dab192007-08-20 18:23:53 +08001944 input_report_key(input, keycode, 1);
1945 input_sync(input);
1946 input_report_key(input, keycode, 0);
1947 input_sync(input);
1948
Patrick Mocheld550d982006-06-27 00:41:40 -04001949 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
Zhang Ruie6d9da12007-08-25 02:23:31 -04001952static int instance;
Matthew Garrett863c1492008-02-04 23:31:24 -08001953static int acpi_video_resume(struct acpi_device *device)
1954{
1955 struct acpi_video_bus *video;
1956 struct acpi_video_device *video_device;
1957 int i;
1958
1959 if (!device || !acpi_driver_data(device))
1960 return -EINVAL;
1961
1962 video = acpi_driver_data(device);
1963
1964 for (i = 0; i < video->attached_count; i++) {
1965 video_device = video->attached_array[i].bind_info;
1966 if (video_device && video_device->backlight)
1967 acpi_video_set_brightness(video_device->backlight);
1968 }
1969 return AE_OK;
1970}
1971
Len Brown4be44fc2005-08-05 00:44:28 -04001972static int acpi_video_bus_add(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001974 acpi_status status;
1975 struct acpi_video_bus *video;
Luming Yue9dab192007-08-20 18:23:53 +08001976 struct input_dev *input;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001977 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Burman Yan36bcbec2006-12-19 12:56:11 -08001979 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (!video)
Patrick Mocheld550d982006-06-27 00:41:40 -04001981 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Zhang Ruie6d9da12007-08-25 02:23:31 -04001983 /* a hack to fix the duplicate name "VID" problem on T61 */
1984 if (!strcmp(device->pnp.bus_id, "VID")) {
1985 if (instance)
1986 device->pnp.bus_id[3] = '0' + instance;
1987 instance ++;
1988 }
1989
Patrick Mochele6afa0d2006-05-19 16:54:40 -04001990 video->device = device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1992 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07001993 device->driver_data = video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 acpi_video_bus_find_cap(video);
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05001996 error = acpi_video_bus_check(video);
1997 if (error)
1998 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002000 error = acpi_video_bus_add_fs(device);
2001 if (error)
2002 goto err_free_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Dmitry Torokhovbbac81f2007-11-05 11:43:32 -05002004 mutex_init(&video->device_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 INIT_LIST_HEAD(&video->video_device_list);
2006
2007 acpi_video_bus_get_devices(video, device);
2008 acpi_video_bus_start_devices(video);
2009
Patrick Mochel90130262006-05-19 16:54:48 -04002010 status = acpi_install_notify_handler(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002011 ACPI_DEVICE_NOTIFY,
2012 acpi_video_bus_notify, video);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (ACPI_FAILURE(status)) {
Lin Ming55ac9a02008-09-28 14:51:56 +08002014 printk(KERN_ERR PREFIX
2015 "Error installing notify handler\n");
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002016 error = -ENODEV;
2017 goto err_stop_video;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
2019
Luming Yue9dab192007-08-20 18:23:53 +08002020 video->input = input = input_allocate_device();
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002021 if (!input) {
2022 error = -ENOMEM;
2023 goto err_uninstall_notify;
2024 }
Luming Yue9dab192007-08-20 18:23:53 +08002025
2026 snprintf(video->phys, sizeof(video->phys),
2027 "%s/video/input0", acpi_device_hid(video->device));
2028
2029 input->name = acpi_device_name(video->device);
2030 input->phys = video->phys;
2031 input->id.bustype = BUS_HOST;
2032 input->id.product = 0x06;
Dmitry Torokhov91c05c62007-11-05 11:43:29 -05002033 input->dev.parent = &device->dev;
Luming Yue9dab192007-08-20 18:23:53 +08002034 input->evbit[0] = BIT(EV_KEY);
2035 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2036 set_bit(KEY_VIDEO_NEXT, input->keybit);
2037 set_bit(KEY_VIDEO_PREV, input->keybit);
2038 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2039 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2040 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2041 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2042 set_bit(KEY_DISPLAY_OFF, input->keybit);
2043 set_bit(KEY_UNKNOWN, input->keybit);
Luming Yue9dab192007-08-20 18:23:53 +08002044
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002045 error = input_register_device(input);
2046 if (error)
2047 goto err_free_input_dev;
Luming Yue9dab192007-08-20 18:23:53 +08002048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
Len Brown4be44fc2005-08-05 00:44:28 -04002050 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2051 video->flags.multihead ? "yes" : "no",
2052 video->flags.rom ? "yes" : "no",
2053 video->flags.post ? "yes" : "no");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002055 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002057 err_free_input_dev:
2058 input_free_device(input);
2059 err_uninstall_notify:
2060 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
2061 acpi_video_bus_notify);
2062 err_stop_video:
2063 acpi_video_bus_stop_devices(video);
2064 acpi_video_bus_put_devices(video);
2065 kfree(video->attached_array);
2066 acpi_video_bus_remove_fs(device);
2067 err_free_video:
2068 kfree(video);
Pavel Machekdb89b4f2008-09-22 14:37:34 -07002069 device->driver_data = NULL;
Dmitry Torokhovf51e8392007-11-05 11:43:30 -05002070
2071 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Len Brown4be44fc2005-08-05 00:44:28 -04002074static int acpi_video_bus_remove(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Len Brown4be44fc2005-08-05 00:44:28 -04002076 acpi_status status = 0;
2077 struct acpi_video_bus *video = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 if (!device || !acpi_driver_data(device))
Patrick Mocheld550d982006-06-27 00:41:40 -04002081 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Jan Engelhardt50dd0962006-10-01 00:28:50 +02002083 video = acpi_driver_data(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 acpi_video_bus_stop_devices(video);
2086
Patrick Mochel90130262006-05-19 16:54:48 -04002087 status = acpi_remove_notify_handler(video->device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04002088 ACPI_DEVICE_NOTIFY,
2089 acpi_video_bus_notify);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 acpi_video_bus_put_devices(video);
2092 acpi_video_bus_remove_fs(device);
2093
Luming Yue9dab192007-08-20 18:23:53 +08002094 input_unregister_device(video->input);
Jesper Juhl6044ec82005-11-07 01:01:32 -08002095 kfree(video->attached_array);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 kfree(video);
2097
Patrick Mocheld550d982006-06-27 00:41:40 -04002098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
Len Brown4be44fc2005-08-05 00:44:28 -04002101static int __init acpi_video_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Len Brown4be44fc2005-08-05 00:44:28 -04002103 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2106 if (!acpi_video_dir)
Patrick Mocheld550d982006-06-27 00:41:40 -04002107 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 acpi_video_dir->owner = THIS_MODULE;
2109
2110 result = acpi_bus_register_driver(&acpi_video_bus);
2111 if (result < 0) {
2112 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
Patrick Mocheld550d982006-06-27 00:41:40 -04002113 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
2115
Patrick Mocheld550d982006-06-27 00:41:40 -04002116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
Len Brown4be44fc2005-08-05 00:44:28 -04002119static void __exit acpi_video_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 acpi_bus_unregister_driver(&acpi_video_bus);
2123
2124 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2125
Patrick Mocheld550d982006-06-27 00:41:40 -04002126 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
2129module_init(acpi_video_init);
2130module_exit(acpi_video_exit);