Input: elantech - discard the first 2 positions on some firmwares
[deliverable/linux.git] / drivers / acpi / video.c
CommitLineData
1da177e4
LT
1/*
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>
f4715189 6 * Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
1da177e4
LT
7 *
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>
bbac81f5 32#include <linux/mutex.h>
1da177e4
LT
33#include <linux/proc_fs.h>
34#include <linux/seq_file.h>
e9dab196 35#include <linux/input.h>
2f3d000a 36#include <linux/backlight.h>
702ed512 37#include <linux/thermal.h>
23b0f015 38#include <linux/video_output.h>
935e5f29 39#include <linux/sort.h>
74a365b3
MG
40#include <linux/pci.h>
41#include <linux/pci_ids.h>
5a0e3ad6 42#include <linux/slab.h>
1da177e4 43#include <asm/uaccess.h>
eb27cae8 44#include <linux/dmi.h>
1da177e4
LT
45#include <acpi/acpi_bus.h>
46#include <acpi/acpi_drivers.h>
ac7729da 47#include <linux/suspend.h>
e92a7162 48#include <acpi/video.h>
1da177e4 49
a192a958
LB
50#define PREFIX "ACPI: "
51
1da177e4 52#define ACPI_VIDEO_CLASS "video"
1da177e4
LT
53#define ACPI_VIDEO_BUS_NAME "Video Bus"
54#define ACPI_VIDEO_DEVICE_NAME "Video Device"
55#define ACPI_VIDEO_NOTIFY_SWITCH 0x80
56#define ACPI_VIDEO_NOTIFY_PROBE 0x81
57#define ACPI_VIDEO_NOTIFY_CYCLE 0x82
58#define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
59#define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
60
f4715189
TT
61#define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
62#define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
63#define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
64#define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
65#define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
1da177e4 66
2f3d000a 67#define MAX_NAME_LEN 20
1da177e4 68
1da177e4 69#define _COMPONENT ACPI_VIDEO_COMPONENT
f52fd66d 70ACPI_MODULE_NAME("video");
1da177e4 71
f52fd66d 72MODULE_AUTHOR("Bruno Ducrot");
7cda93e0 73MODULE_DESCRIPTION("ACPI Video Driver");
1da177e4
LT
74MODULE_LICENSE("GPL");
75
8a681a4d
ZR
76static int brightness_switch_enabled = 1;
77module_param(brightness_switch_enabled, bool, 0644);
78
c504f8cb
ZR
79/*
80 * By default, we don't allow duplicate ACPI video bus devices
81 * under the same VGA controller
82 */
83static int allow_duplicates;
84module_param(allow_duplicates, bool, 0644);
85
86e437f0 86static int register_count = 0;
4be44fcd
LB
87static int acpi_video_bus_add(struct acpi_device *device);
88static int acpi_video_bus_remove(struct acpi_device *device, int type);
7015558f 89static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
1da177e4 90
1ba90e3a
TR
91static const struct acpi_device_id video_device_ids[] = {
92 {ACPI_VIDEO_HID, 0},
93 {"", 0},
94};
95MODULE_DEVICE_TABLE(acpi, video_device_ids);
96
1da177e4 97static struct acpi_driver acpi_video_bus = {
c2b6705b 98 .name = "video",
1da177e4 99 .class = ACPI_VIDEO_CLASS,
1ba90e3a 100 .ids = video_device_ids,
1da177e4
LT
101 .ops = {
102 .add = acpi_video_bus_add,
103 .remove = acpi_video_bus_remove,
7015558f 104 .notify = acpi_video_bus_notify,
4be44fcd 105 },
1da177e4
LT
106};
107
108struct acpi_video_bus_flags {
4be44fcd
LB
109 u8 multihead:1; /* can switch video heads */
110 u8 rom:1; /* can retrieve a video rom */
111 u8 post:1; /* can configure the head to */
112 u8 reserved:5;
1da177e4
LT
113};
114
115struct acpi_video_bus_cap {
4be44fcd
LB
116 u8 _DOS:1; /*Enable/Disable output switching */
117 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
118 u8 _ROM:1; /*Get ROM Data */
119 u8 _GPD:1; /*Get POST Device */
120 u8 _SPD:1; /*Set POST Device */
121 u8 _VPO:1; /*Video POST Options */
122 u8 reserved:2;
1da177e4
LT
123};
124
4be44fcd
LB
125struct acpi_video_device_attrib {
126 u32 display_index:4; /* A zero-based instance of the Display */
98fb8fe1 127 u32 display_port_attachment:4; /*This field differentiates the display type */
4be44fcd 128 u32 display_type:4; /*Describe the specific type in use */
98fb8fe1 129 u32 vendor_specific:4; /*Chipset Vendor Specific */
4be44fcd
LB
130 u32 bios_can_detect:1; /*BIOS can detect the device */
131 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
132 the VGA device. */
133 u32 pipe_id:3; /*For VGA multiple-head devices. */
134 u32 reserved:10; /*Must be 0 */
135 u32 device_id_scheme:1; /*Device ID Scheme */
1da177e4
LT
136};
137
138struct acpi_video_enumerated_device {
139 union {
140 u32 int_val;
4be44fcd 141 struct acpi_video_device_attrib attrib;
1da177e4
LT
142 } value;
143 struct acpi_video_device *bind_info;
144};
145
146struct acpi_video_bus {
e6afa0de 147 struct acpi_device *device;
4be44fcd 148 u8 dos_setting;
1da177e4 149 struct acpi_video_enumerated_device *attached_array;
4be44fcd
LB
150 u8 attached_count;
151 struct acpi_video_bus_cap cap;
1da177e4 152 struct acpi_video_bus_flags flags;
4be44fcd 153 struct list_head video_device_list;
bbac81f5 154 struct mutex device_list_lock; /* protects video_device_list */
4be44fcd 155 struct proc_dir_entry *dir;
e9dab196
LY
156 struct input_dev *input;
157 char phys[32]; /* for input device */
ac7729da 158 struct notifier_block pm_nb;
1da177e4
LT
159};
160
161struct acpi_video_device_flags {
4be44fcd
LB
162 u8 crt:1;
163 u8 lcd:1;
164 u8 tvout:1;
82cae999 165 u8 dvi:1;
4be44fcd
LB
166 u8 bios:1;
167 u8 unknown:1;
82cae999 168 u8 reserved:2;
1da177e4
LT
169};
170
171struct acpi_video_device_cap {
4be44fcd
LB
172 u8 _ADR:1; /*Return the unique ID */
173 u8 _BCL:1; /*Query list of brightness control levels supported */
174 u8 _BCM:1; /*Set the brightness level */
2f3d000a 175 u8 _BQC:1; /* Get current brightness level */
c60d638e 176 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
4be44fcd
LB
177 u8 _DDC:1; /*Return the EDID for this device */
178 u8 _DCS:1; /*Return status of output device */
179 u8 _DGS:1; /*Query graphics state */
180 u8 _DSS:1; /*Device state set */
1da177e4
LT
181};
182
d32f6947
ZR
183struct acpi_video_brightness_flags {
184 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
d80fb99f 185 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
1a7c618a
ZR
186 u8 _BCL_use_index:1; /* levels in _BCL are index values */
187 u8 _BCM_use_index:1; /* input of _BCM is an index value */
188 u8 _BQC_use_index:1; /* _BQC returns an index value */
d32f6947
ZR
189};
190
1da177e4 191struct acpi_video_device_brightness {
4be44fcd
LB
192 int curr;
193 int count;
194 int *levels;
d32f6947 195 struct acpi_video_brightness_flags flags;
1da177e4
LT
196};
197
198struct acpi_video_device {
4be44fcd
LB
199 unsigned long device_id;
200 struct acpi_video_device_flags flags;
201 struct acpi_video_device_cap cap;
202 struct list_head entry;
203 struct acpi_video_bus *video;
204 struct acpi_device *dev;
1da177e4 205 struct acpi_video_device_brightness *brightness;
2f3d000a 206 struct backlight_device *backlight;
4a703a8f 207 struct thermal_cooling_device *cooling_dev;
23b0f015 208 struct output_device *output_dev;
1da177e4
LT
209};
210
1da177e4
LT
211/* bus */
212static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
070d8eb1 213static const struct file_operations acpi_video_bus_info_fops = {
cf7acfab 214 .owner = THIS_MODULE,
4be44fcd
LB
215 .open = acpi_video_bus_info_open_fs,
216 .read = seq_read,
217 .llseek = seq_lseek,
218 .release = single_release,
1da177e4
LT
219};
220
221static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
070d8eb1 222static const struct file_operations acpi_video_bus_ROM_fops = {
cf7acfab 223 .owner = THIS_MODULE,
4be44fcd
LB
224 .open = acpi_video_bus_ROM_open_fs,
225 .read = seq_read,
226 .llseek = seq_lseek,
227 .release = single_release,
1da177e4
LT
228};
229
4be44fcd
LB
230static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
231 struct file *file);
070d8eb1 232static const struct file_operations acpi_video_bus_POST_info_fops = {
cf7acfab 233 .owner = THIS_MODULE,
4be44fcd
LB
234 .open = acpi_video_bus_POST_info_open_fs,
235 .read = seq_read,
236 .llseek = seq_lseek,
237 .release = single_release,
1da177e4
LT
238};
239
240static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
c07c9a78 241static ssize_t acpi_video_bus_write_POST(struct file *file,
b7171ae7
JE
242 const char __user *buffer, size_t count, loff_t *data);
243static const struct file_operations acpi_video_bus_POST_fops = {
cf7acfab 244 .owner = THIS_MODULE,
4be44fcd
LB
245 .open = acpi_video_bus_POST_open_fs,
246 .read = seq_read,
b7171ae7 247 .write = acpi_video_bus_write_POST,
4be44fcd
LB
248 .llseek = seq_lseek,
249 .release = single_release,
1da177e4
LT
250};
251
1da177e4 252static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
c07c9a78 253static ssize_t acpi_video_bus_write_DOS(struct file *file,
b7171ae7
JE
254 const char __user *buffer, size_t count, loff_t *data);
255static const struct file_operations acpi_video_bus_DOS_fops = {
cf7acfab 256 .owner = THIS_MODULE,
4be44fcd
LB
257 .open = acpi_video_bus_DOS_open_fs,
258 .read = seq_read,
b7171ae7 259 .write = acpi_video_bus_write_DOS,
4be44fcd
LB
260 .llseek = seq_lseek,
261 .release = single_release,
1da177e4
LT
262};
263
264/* device */
4be44fcd
LB
265static int acpi_video_device_info_open_fs(struct inode *inode,
266 struct file *file);
070d8eb1 267static const struct file_operations acpi_video_device_info_fops = {
cf7acfab 268 .owner = THIS_MODULE,
4be44fcd
LB
269 .open = acpi_video_device_info_open_fs,
270 .read = seq_read,
271 .llseek = seq_lseek,
272 .release = single_release,
1da177e4
LT
273};
274
4be44fcd
LB
275static int acpi_video_device_state_open_fs(struct inode *inode,
276 struct file *file);
c07c9a78 277static ssize_t acpi_video_device_write_state(struct file *file,
b7171ae7
JE
278 const char __user *buffer, size_t count, loff_t *data);
279static const struct file_operations acpi_video_device_state_fops = {
cf7acfab 280 .owner = THIS_MODULE,
4be44fcd
LB
281 .open = acpi_video_device_state_open_fs,
282 .read = seq_read,
b7171ae7 283 .write = acpi_video_device_write_state,
4be44fcd
LB
284 .llseek = seq_lseek,
285 .release = single_release,
1da177e4
LT
286};
287
4be44fcd
LB
288static int acpi_video_device_brightness_open_fs(struct inode *inode,
289 struct file *file);
c07c9a78 290static ssize_t acpi_video_device_write_brightness(struct file *file,
b7171ae7 291 const char __user *buffer, size_t count, loff_t *data);
828c0950 292static const struct file_operations acpi_video_device_brightness_fops = {
cf7acfab 293 .owner = THIS_MODULE,
4be44fcd
LB
294 .open = acpi_video_device_brightness_open_fs,
295 .read = seq_read,
b7171ae7 296 .write = acpi_video_device_write_brightness,
4be44fcd
LB
297 .llseek = seq_lseek,
298 .release = single_release,
1da177e4
LT
299};
300
4be44fcd
LB
301static int acpi_video_device_EDID_open_fs(struct inode *inode,
302 struct file *file);
070d8eb1 303static const struct file_operations acpi_video_device_EDID_fops = {
cf7acfab 304 .owner = THIS_MODULE,
4be44fcd
LB
305 .open = acpi_video_device_EDID_open_fs,
306 .read = seq_read,
307 .llseek = seq_lseek,
308 .release = single_release,
1da177e4
LT
309};
310
b7171ae7 311static const char device_decode[][30] = {
1da177e4
LT
312 "motherboard VGA device",
313 "PCI VGA device",
314 "AGP VGA device",
315 "UNKNOWN",
316};
317
4be44fcd
LB
318static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
319static void acpi_video_device_rebind(struct acpi_video_bus *video);
320static void acpi_video_device_bind(struct acpi_video_bus *video,
321 struct acpi_video_device *device);
1da177e4 322static int acpi_video_device_enumerate(struct acpi_video_bus *video);
2f3d000a
YL
323static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
324 int level);
325static int acpi_video_device_lcd_get_level_current(
326 struct acpi_video_device *device,
70287db8 327 unsigned long long *level, int init);
4be44fcd
LB
328static int acpi_video_get_next_level(struct acpi_video_device *device,
329 u32 level_current, u32 event);
c8890f90 330static int acpi_video_switch_brightness(struct acpi_video_device *device,
4be44fcd 331 int event);
23b0f015 332static int acpi_video_device_get_state(struct acpi_video_device *device,
27663c58 333 unsigned long long *state);
23b0f015
LY
334static int acpi_video_output_get(struct output_device *od);
335static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
1da177e4 336
2f3d000a
YL
337/*backlight device sysfs support*/
338static int acpi_video_get_brightness(struct backlight_device *bd)
339{
27663c58 340 unsigned long long cur_level;
38531e6f 341 int i;
2f3d000a 342 struct acpi_video_device *vd =
655bfd7a 343 (struct acpi_video_device *)bl_get_data(bd);
c8890f90 344
70287db8 345 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, 0))
c8890f90 346 return -EINVAL;
38531e6f
MG
347 for (i = 2; i < vd->brightness->count; i++) {
348 if (vd->brightness->levels[i] == cur_level)
349 /* The first two entries are special - see page 575
350 of the ACPI spec 3.0 */
351 return i-2;
352 }
353 return 0;
2f3d000a
YL
354}
355
356static int acpi_video_set_brightness(struct backlight_device *bd)
357{
24450c7a 358 int request_level = bd->props.brightness + 2;
2f3d000a 359 struct acpi_video_device *vd =
655bfd7a 360 (struct acpi_video_device *)bl_get_data(bd);
24450c7a
ZR
361
362 return acpi_video_device_lcd_set_level(vd,
363 vd->brightness->levels[request_level]);
2f3d000a
YL
364}
365
599a52d1
RP
366static struct backlight_ops acpi_backlight_ops = {
367 .get_brightness = acpi_video_get_brightness,
368 .update_status = acpi_video_set_brightness,
369};
370
23b0f015
LY
371/*video output device sysfs support*/
372static int acpi_video_output_get(struct output_device *od)
373{
27663c58 374 unsigned long long state;
23b0f015 375 struct acpi_video_device *vd =
60043428 376 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
23b0f015
LY
377 acpi_video_device_get_state(vd, &state);
378 return (int)state;
379}
380
381static int acpi_video_output_set(struct output_device *od)
382{
383 unsigned long state = od->request_state;
384 struct acpi_video_device *vd=
60043428 385 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
23b0f015
LY
386 return acpi_video_device_set_state(vd, state);
387}
388
389static struct output_properties acpi_output_properties = {
390 .set_state = acpi_video_output_set,
391 .get_status = acpi_video_output_get,
392};
702ed512
ZR
393
394
395/* thermal cooling device callbacks */
4a703a8f 396static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
6503e5df 397 long *state)
702ed512 398{
4a703a8f 399 struct acpi_device *device = cooling_dev->devdata;
702ed512
ZR
400 struct acpi_video_device *video = acpi_driver_data(device);
401
6503e5df
MG
402 *state = video->brightness->count - 3;
403 return 0;
702ed512
ZR
404}
405
4a703a8f 406static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
6503e5df 407 long *state)
702ed512 408{
4a703a8f 409 struct acpi_device *device = cooling_dev->devdata;
702ed512 410 struct acpi_video_device *video = acpi_driver_data(device);
27663c58 411 unsigned long long level;
6503e5df 412 int offset;
702ed512 413
70287db8 414 if (acpi_video_device_lcd_get_level_current(video, &level, 0))
c8890f90 415 return -EINVAL;
6503e5df
MG
416 for (offset = 2; offset < video->brightness->count; offset++)
417 if (level == video->brightness->levels[offset]) {
418 *state = video->brightness->count - offset - 1;
419 return 0;
420 }
702ed512
ZR
421
422 return -EINVAL;
423}
424
425static int
4a703a8f 426video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
702ed512 427{
4a703a8f 428 struct acpi_device *device = cooling_dev->devdata;
702ed512
ZR
429 struct acpi_video_device *video = acpi_driver_data(device);
430 int level;
431
432 if ( state >= video->brightness->count - 2)
433 return -EINVAL;
434
435 state = video->brightness->count - state;
436 level = video->brightness->levels[state -1];
437 return acpi_video_device_lcd_set_level(video, level);
438}
439
440static struct thermal_cooling_device_ops video_cooling_ops = {
441 .get_max_state = video_get_max_state,
442 .get_cur_state = video_get_cur_state,
443 .set_cur_state = video_set_cur_state,
444};
445
1da177e4
LT
446/* --------------------------------------------------------------------------
447 Video Management
448 -------------------------------------------------------------------------- */
449
450/* device */
451
452static int
27663c58 453acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
1da177e4 454{
4be44fcd 455 int status;
90130268
PM
456
457 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
1da177e4 458
d550d98d 459 return status;
1da177e4
LT
460}
461
462static int
4be44fcd 463acpi_video_device_get_state(struct acpi_video_device *device,
27663c58 464 unsigned long long *state)
1da177e4 465{
4be44fcd 466 int status;
1da177e4 467
90130268 468 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
1da177e4 469
d550d98d 470 return status;
1da177e4
LT
471}
472
473static int
4be44fcd 474acpi_video_device_set_state(struct acpi_video_device *device, int state)
1da177e4 475{
4be44fcd
LB
476 int status;
477 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
478 struct acpi_object_list args = { 1, &arg0 };
27663c58 479 unsigned long long ret;
1da177e4 480
1da177e4
LT
481
482 arg0.integer.value = state;
90130268 483 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
1da177e4 484
d550d98d 485 return status;
1da177e4
LT
486}
487
488static int
4be44fcd
LB
489acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
490 union acpi_object **levels)
1da177e4 491{
4be44fcd
LB
492 int status;
493 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
494 union acpi_object *obj;
1da177e4 495
1da177e4
LT
496
497 *levels = NULL;
498
90130268 499 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
1da177e4 500 if (!ACPI_SUCCESS(status))
d550d98d 501 return status;
4be44fcd 502 obj = (union acpi_object *)buffer.pointer;
6665bda7 503 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6468463a 504 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
1da177e4
LT
505 status = -EFAULT;
506 goto err;
507 }
508
509 *levels = obj;
510
d550d98d 511 return 0;
1da177e4 512
4be44fcd 513 err:
6044ec88 514 kfree(buffer.pointer);
1da177e4 515
d550d98d 516 return status;
1da177e4
LT
517}
518
519static int
4be44fcd 520acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
1da177e4 521{
24450c7a 522 int status;
4be44fcd
LB
523 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
524 struct acpi_object_list args = { 1, &arg0 };
9e6dada9 525 int state;
1da177e4 526
1da177e4 527 arg0.integer.value = level;
1da177e4 528
24450c7a
ZR
529 status = acpi_evaluate_object(device->dev->handle, "_BCM",
530 &args, NULL);
531 if (ACPI_FAILURE(status)) {
532 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
533 return -EIO;
534 }
535
4500ca8e 536 device->brightness->curr = level;
9e6dada9 537 for (state = 2; state < device->brightness->count; state++)
24450c7a 538 if (level == device->brightness->levels[state]) {
1a7c618a
ZR
539 if (device->backlight)
540 device->backlight->props.brightness = state - 2;
24450c7a
ZR
541 return 0;
542 }
9e6dada9 543
24450c7a
ZR
544 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
545 return -EINVAL;
1da177e4
LT
546}
547
45cb50e6
ZR
548/*
549 * For some buggy _BQC methods, we need to add a constant value to
550 * the _BQC return value to get the actual current brightness level
551 */
552
553static int bqc_offset_aml_bug_workaround;
554static int __init video_set_bqc_offset(const struct dmi_system_id *d)
555{
556 bqc_offset_aml_bug_workaround = 9;
557 return 0;
558}
559
560static struct dmi_system_id video_dmi_table[] __initdata = {
561 /*
562 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
563 */
564 {
565 .callback = video_set_bqc_offset,
566 .ident = "Acer Aspire 5720",
567 .matches = {
568 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
569 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
570 },
571 },
5afc4abe
LB
572 {
573 .callback = video_set_bqc_offset,
574 .ident = "Acer Aspire 5710Z",
575 .matches = {
576 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
577 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
578 },
579 },
34ac272b
ZR
580 {
581 .callback = video_set_bqc_offset,
582 .ident = "eMachines E510",
583 .matches = {
584 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
585 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
586 },
587 },
93bcece2
ZR
588 {
589 .callback = video_set_bqc_offset,
590 .ident = "Acer Aspire 5315",
591 .matches = {
592 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
593 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
594 },
595 },
152a4e63
ZR
596 {
597 .callback = video_set_bqc_offset,
598 .ident = "Acer Aspire 7720",
599 .matches = {
600 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
601 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
602 },
603 },
45cb50e6
ZR
604 {}
605};
606
1da177e4 607static int
4be44fcd 608acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
70287db8 609 unsigned long long *level, int init)
1da177e4 610{
c8890f90 611 acpi_status status = AE_OK;
4e231fa4 612 int i;
c8890f90 613
c60d638e
ZR
614 if (device->cap._BQC || device->cap._BCQ) {
615 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
616
617 status = acpi_evaluate_integer(device->dev->handle, buf,
c8890f90
ZR
618 NULL, level);
619 if (ACPI_SUCCESS(status)) {
1a7c618a
ZR
620 if (device->brightness->flags._BQC_use_index) {
621 if (device->brightness->flags._BCL_reversed)
622 *level = device->brightness->count
623 - 3 - (*level);
624 *level = device->brightness->levels[*level + 2];
625
626 }
45cb50e6 627 *level += bqc_offset_aml_bug_workaround;
4e231fa4
VS
628 for (i = 2; i < device->brightness->count; i++)
629 if (device->brightness->levels[i] == *level) {
630 device->brightness->curr = *level;
631 return 0;
632 }
70287db8
MG
633 if (!init) {
634 /*
635 * BQC returned an invalid level.
636 * Stop using it.
637 */
638 ACPI_WARNING((AE_INFO,
639 "%s returned an invalid level",
640 buf));
641 device->cap._BQC = device->cap._BCQ = 0;
642 }
c8890f90
ZR
643 } else {
644 /* Fixme:
645 * should we return an error or ignore this failure?
646 * dev->brightness->curr is a cached value which stores
647 * the correct current backlight level in most cases.
648 * ACPI video backlight still works w/ buggy _BQC.
649 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
650 */
c60d638e
ZR
651 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
652 device->cap._BQC = device->cap._BCQ = 0;
c8890f90
ZR
653 }
654 }
655
4500ca8e 656 *level = device->brightness->curr;
c8890f90 657 return 0;
1da177e4
LT
658}
659
660static int
4be44fcd
LB
661acpi_video_device_EDID(struct acpi_video_device *device,
662 union acpi_object **edid, ssize_t length)
1da177e4 663{
4be44fcd
LB
664 int status;
665 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
666 union acpi_object *obj;
667 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
668 struct acpi_object_list args = { 1, &arg0 };
1da177e4 669
1da177e4
LT
670
671 *edid = NULL;
672
673 if (!device)
d550d98d 674 return -ENODEV;
1da177e4
LT
675 if (length == 128)
676 arg0.integer.value = 1;
677 else if (length == 256)
678 arg0.integer.value = 2;
679 else
d550d98d 680 return -EINVAL;
1da177e4 681
90130268 682 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
1da177e4 683 if (ACPI_FAILURE(status))
d550d98d 684 return -ENODEV;
1da177e4 685
50dd0969 686 obj = buffer.pointer;
1da177e4
LT
687
688 if (obj && obj->type == ACPI_TYPE_BUFFER)
689 *edid = obj;
690 else {
6468463a 691 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
1da177e4
LT
692 status = -EFAULT;
693 kfree(obj);
694 }
695
d550d98d 696 return status;
1da177e4
LT
697}
698
1da177e4
LT
699/* bus */
700
701static int
4be44fcd 702acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
1da177e4 703{
4be44fcd 704 int status;
27663c58 705 unsigned long long tmp;
4be44fcd
LB
706 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
707 struct acpi_object_list args = { 1, &arg0 };
1da177e4 708
1da177e4
LT
709
710 arg0.integer.value = option;
711
90130268 712 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
1da177e4 713 if (ACPI_SUCCESS(status))
4be44fcd 714 status = tmp ? (-EINVAL) : (AE_OK);
1da177e4 715
d550d98d 716 return status;
1da177e4
LT
717}
718
719static int
27663c58 720acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
1da177e4
LT
721{
722 int status;
723
90130268 724 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
1da177e4 725
d550d98d 726 return status;
1da177e4
LT
727}
728
729static int
4be44fcd 730acpi_video_bus_POST_options(struct acpi_video_bus *video,
27663c58 731 unsigned long long *options)
1da177e4 732{
4be44fcd 733 int status;
1da177e4 734
90130268 735 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
1da177e4
LT
736 *options &= 3;
737
d550d98d 738 return status;
1da177e4
LT
739}
740
741/*
742 * Arg:
743 * video : video bus device pointer
744 * bios_flag :
745 * 0. The system BIOS should NOT automatically switch(toggle)
746 * the active display output.
747 * 1. The system BIOS should automatically switch (toggle) the
98fb8fe1 748 * active display output. No switch event.
1da177e4
LT
749 * 2. The _DGS value should be locked.
750 * 3. The system BIOS should not automatically switch (toggle) the
751 * active display output, but instead generate the display switch
752 * event notify code.
753 * lcd_flag :
754 * 0. The system BIOS should automatically control the brightness level
98fb8fe1 755 * of the LCD when the power changes from AC to DC
1da177e4 756 * 1. The system BIOS should NOT automatically control the brightness
98fb8fe1 757 * level of the LCD when the power changes from AC to DC.
1da177e4
LT
758 * Return Value:
759 * -1 wrong arg.
760 */
761
762static int
4be44fcd 763acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
1da177e4 764{
439913ff 765 u64 status = 0;
4be44fcd
LB
766 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
767 struct acpi_object_list args = { 1, &arg0 };
1da177e4 768
1da177e4 769
4be44fcd 770 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
1da177e4
LT
771 status = -1;
772 goto Failed;
773 }
774 arg0.integer.value = (lcd_flag << 2) | bios_flag;
775 video->dos_setting = arg0.integer.value;
90130268 776 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
1da177e4 777
4be44fcd 778 Failed:
d550d98d 779 return status;
1da177e4
LT
780}
781
935e5f29
ZR
782/*
783 * Simple comparison function used to sort backlight levels.
784 */
785
786static int
787acpi_video_cmp_level(const void *a, const void *b)
788{
789 return *(int *)a - *(int *)b;
790}
791
1da177e4
LT
792/*
793 * Arg:
794 * device : video output device (LCD, CRT, ..)
795 *
796 * Return Value:
469778c1
JJ
797 * Maximum brightness level
798 *
799 * Allocate and initialize device->brightness.
800 */
801
802static int
803acpi_video_init_brightness(struct acpi_video_device *device)
804{
805 union acpi_object *obj = NULL;
d32f6947 806 int i, max_level = 0, count = 0, level_ac_battery = 0;
1a7c618a 807 unsigned long long level, level_old;
469778c1
JJ
808 union acpi_object *o;
809 struct acpi_video_device_brightness *br = NULL;
1a7c618a 810 int result = -EINVAL;
469778c1
JJ
811
812 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
813 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
814 "LCD brightness level\n"));
815 goto out;
816 }
817
818 if (obj->package.count < 2)
819 goto out;
820
821 br = kzalloc(sizeof(*br), GFP_KERNEL);
822 if (!br) {
823 printk(KERN_ERR "can't allocate memory\n");
1a7c618a 824 result = -ENOMEM;
469778c1
JJ
825 goto out;
826 }
827
d32f6947 828 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
469778c1 829 GFP_KERNEL);
1a7c618a
ZR
830 if (!br->levels) {
831 result = -ENOMEM;
469778c1 832 goto out_free;
1a7c618a 833 }
469778c1
JJ
834
835 for (i = 0; i < obj->package.count; i++) {
836 o = (union acpi_object *)&obj->package.elements[i];
837 if (o->type != ACPI_TYPE_INTEGER) {
838 printk(KERN_ERR PREFIX "Invalid data\n");
839 continue;
840 }
841 br->levels[count] = (u32) o->integer.value;
842
843 if (br->levels[count] > max_level)
844 max_level = br->levels[count];
845 count++;
846 }
847
d32f6947
ZR
848 /*
849 * some buggy BIOS don't export the levels
850 * when machine is on AC/Battery in _BCL package.
851 * In this case, the first two elements in _BCL packages
852 * are also supported brightness levels that OS should take care of.
853 */
90af2cf6
ZR
854 for (i = 2; i < count; i++) {
855 if (br->levels[i] == br->levels[0])
d32f6947 856 level_ac_battery++;
90af2cf6
ZR
857 if (br->levels[i] == br->levels[1])
858 level_ac_battery++;
859 }
d32f6947
ZR
860
861 if (level_ac_battery < 2) {
862 level_ac_battery = 2 - level_ac_battery;
863 br->flags._BCL_no_ac_battery_levels = 1;
864 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
865 br->levels[i] = br->levels[i - level_ac_battery];
866 count += level_ac_battery;
867 } else if (level_ac_battery > 2)
868 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
869
d80fb99f
ZR
870 /* Check if the _BCL package is in a reversed order */
871 if (max_level == br->levels[2]) {
872 br->flags._BCL_reversed = 1;
873 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
874 acpi_video_cmp_level, NULL);
875 } else if (max_level != br->levels[count - 1])
876 ACPI_ERROR((AE_INFO,
877 "Found unordered _BCL package\n"));
469778c1
JJ
878
879 br->count = count;
880 device->brightness = br;
1a7c618a
ZR
881
882 /* Check the input/output of _BQC/_BCL/_BCM */
883 if ((max_level < 100) && (max_level <= (count - 2)))
884 br->flags._BCL_use_index = 1;
885
886 /*
887 * _BCM is always consistent with _BCL,
888 * at least for all the laptops we have ever seen.
889 */
890 br->flags._BCM_use_index = br->flags._BCL_use_index;
891
892 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
90c53ca4 893 br->curr = level = max_level;
e047cca6
ZR
894
895 if (!device->cap._BQC)
896 goto set_level;
897
70287db8 898 result = acpi_video_device_lcd_get_level_current(device, &level_old, 1);
1a7c618a
ZR
899 if (result)
900 goto out_free_levels;
901
e047cca6
ZR
902 /*
903 * Set the level to maximum and check if _BQC uses indexed value
904 */
905 result = acpi_video_device_lcd_set_level(device, max_level);
1a7c618a
ZR
906 if (result)
907 goto out_free_levels;
908
70287db8 909 result = acpi_video_device_lcd_get_level_current(device, &level, 0);
1a7c618a
ZR
910 if (result)
911 goto out_free_levels;
912
e047cca6
ZR
913 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
914
90c53ca4
ZR
915 if (!br->flags._BQC_use_index) {
916 /*
917 * Set the backlight to the initial state.
918 * On some buggy laptops, _BQC returns an uninitialized value
919 * when invoked for the first time, i.e. level_old is invalid.
920 * set the backlight to max_level in this case
921 */
922 for (i = 2; i < br->count; i++)
923 if (level_old == br->levels[i])
924 level = level_old;
e047cca6 925 goto set_level;
90c53ca4 926 }
e047cca6
ZR
927
928 if (br->flags._BCL_reversed)
929 level_old = (br->count - 1) - level_old;
90c53ca4 930 level = br->levels[level_old];
e047cca6
ZR
931
932set_level:
90c53ca4 933 result = acpi_video_device_lcd_set_level(device, level);
e047cca6
ZR
934 if (result)
935 goto out_free_levels;
1a7c618a 936
d32f6947
ZR
937 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
938 "found %d brightness levels\n", count - 2));
469778c1 939 kfree(obj);
1a7c618a 940 return result;
469778c1
JJ
941
942out_free_levels:
943 kfree(br->levels);
944out_free:
945 kfree(br);
946out:
947 device->brightness = NULL;
948 kfree(obj);
1a7c618a 949 return result;
469778c1
JJ
950}
951
952/*
953 * Arg:
954 * device : video output device (LCD, CRT, ..)
955 *
956 * Return Value:
1da177e4
LT
957 * None
958 *
98fb8fe1 959 * Find out all required AML methods defined under the output
1da177e4
LT
960 * device.
961 */
962
4be44fcd 963static void acpi_video_device_find_cap(struct acpi_video_device *device)
1da177e4 964{
1da177e4 965 acpi_handle h_dummy1;
1da177e4 966
90130268 967 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
1da177e4
LT
968 device->cap._ADR = 1;
969 }
90130268 970 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
4be44fcd 971 device->cap._BCL = 1;
1da177e4 972 }
90130268 973 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
4be44fcd 974 device->cap._BCM = 1;
1da177e4 975 }
2f3d000a
YL
976 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
977 device->cap._BQC = 1;
c60d638e
ZR
978 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
979 &h_dummy1))) {
980 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
981 device->cap._BCQ = 1;
982 }
983
90130268 984 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
4be44fcd 985 device->cap._DDC = 1;
1da177e4 986 }
90130268 987 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
1da177e4
LT
988 device->cap._DCS = 1;
989 }
90130268 990 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
1da177e4
LT
991 device->cap._DGS = 1;
992 }
90130268 993 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
1da177e4
LT
994 device->cap._DSS = 1;
995 }
996
1a7c618a 997 if (acpi_video_backlight_support()) {
a19a6ee6 998 struct backlight_properties props;
702ed512 999 int result;
2f3d000a
YL
1000 static int count = 0;
1001 char *name;
1a7c618a
ZR
1002
1003 result = acpi_video_init_brightness(device);
1004 if (result)
1005 return;
aeb834d9 1006 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
2f3d000a
YL
1007 if (!name)
1008 return;
aeb834d9 1009 count++;
2f3d000a 1010
a19a6ee6
MG
1011 memset(&props, 0, sizeof(struct backlight_properties));
1012 props.max_brightness = device->brightness->count - 3;
1013 device->backlight = backlight_device_register(name, NULL, device,
1014 &acpi_backlight_ops,
1015 &props);
2f3d000a 1016 kfree(name);
e01ce79b
ZR
1017 if (IS_ERR(device->backlight))
1018 return;
702ed512 1019
ac7729da
RW
1020 /*
1021 * Save current brightness level in case we have to restore it
1022 * before acpi_video_device_lcd_set_level() is called next time.
1023 */
1024 device->backlight->props.brightness =
1025 acpi_video_get_brightness(device->backlight);
702ed512 1026
056c308d
ZR
1027 result = sysfs_create_link(&device->backlight->dev.kobj,
1028 &device->dev->dev.kobj, "device");
1029 if (result)
1030 printk(KERN_ERR PREFIX "Create sysfs link\n");
1031
4a703a8f 1032 device->cooling_dev = thermal_cooling_device_register("LCD",
702ed512 1033 device->dev, &video_cooling_ops);
4a703a8f 1034 if (IS_ERR(device->cooling_dev)) {
4b4fe3b6 1035 /*
4a703a8f 1036 * Set cooling_dev to NULL so we don't crash trying to
4b4fe3b6
DT
1037 * free it.
1038 * Also, why the hell we are returning early and
1039 * not attempt to register video output if cooling
1040 * device registration failed?
1041 * -- dtor
1042 */
4a703a8f 1043 device->cooling_dev = NULL;
43ff39f2 1044 return;
4b4fe3b6 1045 }
43ff39f2 1046
fc3a8828 1047 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
4a703a8f 1048 device->cooling_dev->id);
9030062f 1049 result = sysfs_create_link(&device->dev->dev.kobj,
4a703a8f 1050 &device->cooling_dev->device.kobj,
9030062f
JL
1051 "thermal_cooling");
1052 if (result)
1053 printk(KERN_ERR PREFIX "Create sysfs link\n");
4a703a8f 1054 result = sysfs_create_link(&device->cooling_dev->device.kobj,
9030062f
JL
1055 &device->dev->dev.kobj, "device");
1056 if (result)
1057 printk(KERN_ERR PREFIX "Create sysfs link\n");
1058
2f3d000a 1059 }
c3d6de69
TR
1060
1061 if (acpi_video_display_switch_support()) {
1062
1063 if (device->cap._DCS && device->cap._DSS) {
1064 static int count;
1065 char *name;
aeb834d9 1066 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
c3d6de69
TR
1067 if (!name)
1068 return;
aeb834d9 1069 count++;
c3d6de69
TR
1070 device->output_dev = video_output_register(name,
1071 NULL, device, &acpi_output_properties);
1072 kfree(name);
1073 }
23b0f015 1074 }
1da177e4
LT
1075}
1076
1077/*
1078 * Arg:
1079 * device : video output device (VGA)
1080 *
1081 * Return Value:
1082 * None
1083 *
98fb8fe1 1084 * Find out all required AML methods defined under the video bus device.
1da177e4
LT
1085 */
1086
4be44fcd 1087static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
1da177e4 1088{
4be44fcd 1089 acpi_handle h_dummy1;
1da177e4 1090
90130268 1091 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
1da177e4
LT
1092 video->cap._DOS = 1;
1093 }
90130268 1094 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
1da177e4
LT
1095 video->cap._DOD = 1;
1096 }
90130268 1097 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
1da177e4
LT
1098 video->cap._ROM = 1;
1099 }
90130268 1100 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
1da177e4
LT
1101 video->cap._GPD = 1;
1102 }
90130268 1103 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
1da177e4
LT
1104 video->cap._SPD = 1;
1105 }
90130268 1106 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
1da177e4
LT
1107 video->cap._VPO = 1;
1108 }
1109}
1110
1111/*
1112 * Check whether the video bus device has required AML method to
1113 * support the desired features
1114 */
1115
4be44fcd 1116static int acpi_video_bus_check(struct acpi_video_bus *video)
1da177e4 1117{
4be44fcd 1118 acpi_status status = -ENOENT;
1e4cffe7 1119 struct pci_dev *dev;
1da177e4
LT
1120
1121 if (!video)
d550d98d 1122 return -EINVAL;
1da177e4 1123
1e4cffe7 1124 dev = acpi_get_pci_dev(video->device->handle);
22c13f9d
TR
1125 if (!dev)
1126 return -ENODEV;
1e4cffe7 1127 pci_dev_put(dev);
22c13f9d 1128
1da177e4
LT
1129 /* Since there is no HID, CID and so on for VGA driver, we have
1130 * to check well known required nodes.
1131 */
1132
98fb8fe1 1133 /* Does this device support video switching? */
3a1151e3
SB
1134 if (video->cap._DOS || video->cap._DOD) {
1135 if (!video->cap._DOS) {
1136 printk(KERN_WARNING FW_BUG
1137 "ACPI(%s) defines _DOD but not _DOS\n",
1138 acpi_device_bid(video->device));
1139 }
1da177e4
LT
1140 video->flags.multihead = 1;
1141 status = 0;
1142 }
1143
98fb8fe1 1144 /* Does this device support retrieving a video ROM? */
4be44fcd 1145 if (video->cap._ROM) {
1da177e4
LT
1146 video->flags.rom = 1;
1147 status = 0;
1148 }
1149
98fb8fe1 1150 /* Does this device support configuring which video device to POST? */
4be44fcd 1151 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
1da177e4
LT
1152 video->flags.post = 1;
1153 status = 0;
1154 }
1155
d550d98d 1156 return status;
1da177e4
LT
1157}
1158
1159/* --------------------------------------------------------------------------
1160 FS Interface (/proc)
1161 -------------------------------------------------------------------------- */
1162
4be44fcd 1163static struct proc_dir_entry *acpi_video_dir;
1da177e4
LT
1164
1165/* video devices */
1166
4be44fcd 1167static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
1da177e4 1168{
50dd0969 1169 struct acpi_video_device *dev = seq->private;
1da177e4 1170
1da177e4
LT
1171
1172 if (!dev)
1173 goto end;
1174
1175 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1176 seq_printf(seq, "type: ");
1177 if (dev->flags.crt)
1178 seq_printf(seq, "CRT\n");
1179 else if (dev->flags.lcd)
1180 seq_printf(seq, "LCD\n");
1181 else if (dev->flags.tvout)
1182 seq_printf(seq, "TVOUT\n");
82cae999
RZ
1183 else if (dev->flags.dvi)
1184 seq_printf(seq, "DVI\n");
1da177e4
LT
1185 else
1186 seq_printf(seq, "UNKNOWN\n");
1187
4be44fcd 1188 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
1da177e4 1189
4be44fcd 1190 end:
d550d98d 1191 return 0;
1da177e4
LT
1192}
1193
1194static int
4be44fcd 1195acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
1da177e4
LT
1196{
1197 return single_open(file, acpi_video_device_info_seq_show,
1198 PDE(inode)->data);
1199}
1200
4be44fcd 1201static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
1da177e4 1202{
4be44fcd 1203 int status;
50dd0969 1204 struct acpi_video_device *dev = seq->private;
27663c58 1205 unsigned long long state;
1da177e4 1206
1da177e4
LT
1207
1208 if (!dev)
1209 goto end;
1210
1211 status = acpi_video_device_get_state(dev, &state);
1212 seq_printf(seq, "state: ");
1213 if (ACPI_SUCCESS(status))
27663c58 1214 seq_printf(seq, "0x%02llx\n", state);
1da177e4
LT
1215 else
1216 seq_printf(seq, "<not supported>\n");
1217
1218 status = acpi_video_device_query(dev, &state);
1219 seq_printf(seq, "query: ");
1220 if (ACPI_SUCCESS(status))
27663c58 1221 seq_printf(seq, "0x%02llx\n", state);
1da177e4
LT
1222 else
1223 seq_printf(seq, "<not supported>\n");
1224
4be44fcd 1225 end:
d550d98d 1226 return 0;
1da177e4
LT
1227}
1228
1229static int
4be44fcd 1230acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
1da177e4
LT
1231{
1232 return single_open(file, acpi_video_device_state_seq_show,
1233 PDE(inode)->data);
1234}
1235
1236static ssize_t
4be44fcd
LB
1237acpi_video_device_write_state(struct file *file,
1238 const char __user * buffer,
1239 size_t count, loff_t * data)
1da177e4 1240{
4be44fcd 1241 int status;
50dd0969
JE
1242 struct seq_file *m = file->private_data;
1243 struct acpi_video_device *dev = m->private;
4be44fcd
LB
1244 char str[12] = { 0 };
1245 u32 state = 0;
1da177e4 1246
1da177e4 1247
52a2b11c 1248 if (!dev || count >= sizeof(str))
d550d98d 1249 return -EINVAL;
1da177e4
LT
1250
1251 if (copy_from_user(str, buffer, count))
d550d98d 1252 return -EFAULT;
1da177e4
LT
1253
1254 str[count] = 0;
1255 state = simple_strtoul(str, NULL, 0);
4be44fcd 1256 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
1da177e4
LT
1257
1258 status = acpi_video_device_set_state(dev, state);
1259
1260 if (status)
d550d98d 1261 return -EFAULT;
1da177e4 1262
d550d98d 1263 return count;
1da177e4
LT
1264}
1265
1266static int
4be44fcd 1267acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
1da177e4 1268{
50dd0969 1269 struct acpi_video_device *dev = seq->private;
4be44fcd 1270 int i;
1da177e4 1271
1da177e4
LT
1272
1273 if (!dev || !dev->brightness) {
1274 seq_printf(seq, "<not supported>\n");
d550d98d 1275 return 0;
1da177e4
LT
1276 }
1277
1278 seq_printf(seq, "levels: ");
0a3db1ce 1279 for (i = 2; i < dev->brightness->count; i++)
1da177e4
LT
1280 seq_printf(seq, " %d", dev->brightness->levels[i]);
1281 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1282
d550d98d 1283 return 0;
1da177e4
LT
1284}
1285
1286static int
4be44fcd 1287acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
1da177e4
LT
1288{
1289 return single_open(file, acpi_video_device_brightness_seq_show,
1290 PDE(inode)->data);
1291}
1292
1293static ssize_t
4be44fcd
LB
1294acpi_video_device_write_brightness(struct file *file,
1295 const char __user * buffer,
1296 size_t count, loff_t * data)
1da177e4 1297{
50dd0969
JE
1298 struct seq_file *m = file->private_data;
1299 struct acpi_video_device *dev = m->private;
c88c5786 1300 char str[5] = { 0 };
4be44fcd
LB
1301 unsigned int level = 0;
1302 int i;
1da177e4 1303
1da177e4 1304
52a2b11c 1305 if (!dev || !dev->brightness || count >= sizeof(str))
d550d98d 1306 return -EINVAL;
1da177e4
LT
1307
1308 if (copy_from_user(str, buffer, count))
d550d98d 1309 return -EFAULT;
1da177e4
LT
1310
1311 str[count] = 0;
1312 level = simple_strtoul(str, NULL, 0);
4be44fcd 1313
1da177e4 1314 if (level > 100)
d550d98d 1315 return -EFAULT;
1da177e4 1316
98fb8fe1 1317 /* validate through the list of available levels */
0a3db1ce 1318 for (i = 2; i < dev->brightness->count; i++)
1da177e4 1319 if (level == dev->brightness->levels[i]) {
24450c7a
ZR
1320 if (!acpi_video_device_lcd_set_level(dev, level))
1321 return count;
1da177e4
LT
1322 break;
1323 }
1324
24450c7a 1325 return -EINVAL;
1da177e4
LT
1326}
1327
4be44fcd 1328static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
1da177e4 1329{
50dd0969 1330 struct acpi_video_device *dev = seq->private;
4be44fcd
LB
1331 int status;
1332 int i;
1333 union acpi_object *edid = NULL;
1da177e4 1334
1da177e4
LT
1335
1336 if (!dev)
1337 goto out;
1338
4be44fcd 1339 status = acpi_video_device_EDID(dev, &edid, 128);
1da177e4 1340 if (ACPI_FAILURE(status)) {
4be44fcd 1341 status = acpi_video_device_EDID(dev, &edid, 256);
1da177e4
LT
1342 }
1343
1344 if (ACPI_FAILURE(status)) {
1345 goto out;
1346 }
1347
1348 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1349 for (i = 0; i < edid->buffer.length; i++)
1350 seq_putc(seq, edid->buffer.pointer[i]);
1351 }
1352
4be44fcd 1353 out:
1da177e4
LT
1354 if (!edid)
1355 seq_printf(seq, "<not supported>\n");
1356 else
1357 kfree(edid);
1358
d550d98d 1359 return 0;
1da177e4
LT
1360}
1361
1362static int
4be44fcd 1363acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
1da177e4
LT
1364{
1365 return single_open(file, acpi_video_device_EDID_seq_show,
1366 PDE(inode)->data);
1367}
1368
4be44fcd 1369static int acpi_video_device_add_fs(struct acpi_device *device)
1da177e4 1370{
251cb0bc 1371 struct proc_dir_entry *entry, *device_dir;
1da177e4
LT
1372 struct acpi_video_device *vid_dev;
1373
50dd0969 1374 vid_dev = acpi_driver_data(device);
1da177e4 1375 if (!vid_dev)
d550d98d 1376 return -ENODEV;
1da177e4 1377
251cb0bc
DT
1378 device_dir = proc_mkdir(acpi_device_bid(device),
1379 vid_dev->video->dir);
1380 if (!device_dir)
1381 return -ENOMEM;
1382
1da177e4 1383 /* 'info' [R] */
e0066c4e 1384 entry = proc_create_data("info", S_IRUGO, device_dir,
cf7acfab 1385 &acpi_video_device_info_fops, acpi_driver_data(device));
1da177e4 1386 if (!entry)
251cb0bc 1387 goto err_remove_dir;
1da177e4
LT
1388
1389 /* 'state' [R/W] */
cf7acfab 1390 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
e0066c4e 1391 device_dir,
cf7acfab
DL
1392 &acpi_video_device_state_fops,
1393 acpi_driver_data(device));
1da177e4 1394 if (!entry)
251cb0bc 1395 goto err_remove_info;
1da177e4
LT
1396
1397 /* 'brightness' [R/W] */
cf7acfab 1398 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
e0066c4e 1399 device_dir,
cf7acfab
DL
1400 &acpi_video_device_brightness_fops,
1401 acpi_driver_data(device));
1da177e4 1402 if (!entry)
251cb0bc 1403 goto err_remove_state;
1da177e4
LT
1404
1405 /* 'EDID' [R] */
e0066c4e 1406 entry = proc_create_data("EDID", S_IRUGO, device_dir,
cf7acfab
DL
1407 &acpi_video_device_EDID_fops,
1408 acpi_driver_data(device));
1da177e4 1409 if (!entry)
251cb0bc 1410 goto err_remove_brightness;
1da177e4 1411
e0066c4e
AD
1412 acpi_device_dir(device) = device_dir;
1413
d550d98d 1414 return 0;
251cb0bc
DT
1415
1416 err_remove_brightness:
1417 remove_proc_entry("brightness", device_dir);
1418 err_remove_state:
1419 remove_proc_entry("state", device_dir);
1420 err_remove_info:
1421 remove_proc_entry("info", device_dir);
1422 err_remove_dir:
1423 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1424 return -ENOMEM;
1da177e4
LT
1425}
1426
4be44fcd 1427static int acpi_video_device_remove_fs(struct acpi_device *device)
1da177e4
LT
1428{
1429 struct acpi_video_device *vid_dev;
251cb0bc 1430 struct proc_dir_entry *device_dir;
1da177e4 1431
50dd0969 1432 vid_dev = acpi_driver_data(device);
1da177e4 1433 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
d550d98d 1434 return -ENODEV;
1da177e4 1435
251cb0bc
DT
1436 device_dir = acpi_device_dir(device);
1437 if (device_dir) {
1438 remove_proc_entry("info", device_dir);
1439 remove_proc_entry("state", device_dir);
1440 remove_proc_entry("brightness", device_dir);
1441 remove_proc_entry("EDID", device_dir);
4be44fcd 1442 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1da177e4
LT
1443 acpi_device_dir(device) = NULL;
1444 }
1445
d550d98d 1446 return 0;
1da177e4
LT
1447}
1448
1da177e4 1449/* video bus */
4be44fcd 1450static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
1da177e4 1451{
50dd0969 1452 struct acpi_video_bus *video = seq->private;
1da177e4 1453
1da177e4
LT
1454
1455 if (!video)
1456 goto end;
1457
1458 seq_printf(seq, "Switching heads: %s\n",
4be44fcd 1459 video->flags.multihead ? "yes" : "no");
1da177e4 1460 seq_printf(seq, "Video ROM: %s\n",
4be44fcd 1461 video->flags.rom ? "yes" : "no");
1da177e4 1462 seq_printf(seq, "Device to be POSTed on boot: %s\n",
4be44fcd 1463 video->flags.post ? "yes" : "no");
1da177e4 1464
4be44fcd 1465 end:
d550d98d 1466 return 0;
1da177e4
LT
1467}
1468
4be44fcd 1469static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
1da177e4 1470{
4be44fcd
LB
1471 return single_open(file, acpi_video_bus_info_seq_show,
1472 PDE(inode)->data);
1da177e4
LT
1473}
1474
4be44fcd 1475static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
1da177e4 1476{
50dd0969 1477 struct acpi_video_bus *video = seq->private;
1da177e4 1478
1da177e4
LT
1479
1480 if (!video)
1481 goto end;
1482
96b2dd1f 1483 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
1da177e4
LT
1484 seq_printf(seq, "<TODO>\n");
1485
4be44fcd 1486 end:
d550d98d 1487 return 0;
1da177e4
LT
1488}
1489
4be44fcd 1490static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
1da177e4
LT
1491{
1492 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1493}
1494
4be44fcd 1495static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
1da177e4 1496{
50dd0969 1497 struct acpi_video_bus *video = seq->private;
27663c58 1498 unsigned long long options;
4be44fcd 1499 int status;
1da177e4 1500
1da177e4
LT
1501
1502 if (!video)
1503 goto end;
1504
1505 status = acpi_video_bus_POST_options(video, &options);
1506 if (ACPI_SUCCESS(status)) {
1507 if (!(options & 1)) {
4be44fcd
LB
1508 printk(KERN_WARNING PREFIX
1509 "The motherboard VGA device is not listed as a possible POST device.\n");
1510 printk(KERN_WARNING PREFIX
98fb8fe1 1511 "This indicates a BIOS bug. Please contact the manufacturer.\n");
1da177e4 1512 }
4d939155 1513 printk(KERN_WARNING "%llx\n", options);
98fb8fe1 1514 seq_printf(seq, "can POST: <integrated video>");
1da177e4
LT
1515 if (options & 2)
1516 seq_printf(seq, " <PCI video>");
1517 if (options & 4)
1518 seq_printf(seq, " <AGP video>");
1519 seq_putc(seq, '\n');
1520 } else
1521 seq_printf(seq, "<not supported>\n");
4be44fcd 1522 end:
d550d98d 1523 return 0;
1da177e4
LT
1524}
1525
1526static int
4be44fcd 1527acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
1da177e4 1528{
4be44fcd
LB
1529 return single_open(file, acpi_video_bus_POST_info_seq_show,
1530 PDE(inode)->data);
1da177e4
LT
1531}
1532
4be44fcd 1533static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
1da177e4 1534{
50dd0969 1535 struct acpi_video_bus *video = seq->private;
4be44fcd 1536 int status;
27663c58 1537 unsigned long long id;
1da177e4 1538
1da177e4
LT
1539
1540 if (!video)
1541 goto end;
1542
4be44fcd 1543 status = acpi_video_bus_get_POST(video, &id);
1da177e4
LT
1544 if (!ACPI_SUCCESS(status)) {
1545 seq_printf(seq, "<not supported>\n");
1546 goto end;
1547 }
98fb8fe1 1548 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
1da177e4 1549
4be44fcd 1550 end:
d550d98d 1551 return 0;
1da177e4
LT
1552}
1553
4be44fcd 1554static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
1da177e4 1555{
50dd0969 1556 struct acpi_video_bus *video = seq->private;
1da177e4 1557
1da177e4 1558
4be44fcd 1559 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
1da177e4 1560
d550d98d 1561 return 0;
1da177e4
LT
1562}
1563
4be44fcd 1564static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
1da177e4 1565{
4be44fcd
LB
1566 return single_open(file, acpi_video_bus_POST_seq_show,
1567 PDE(inode)->data);
1da177e4
LT
1568}
1569
4be44fcd 1570static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
1da177e4
LT
1571{
1572 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1573}
1574
1575static ssize_t
4be44fcd
LB
1576acpi_video_bus_write_POST(struct file *file,
1577 const char __user * buffer,
1578 size_t count, loff_t * data)
1da177e4 1579{
4be44fcd 1580 int status;
50dd0969
JE
1581 struct seq_file *m = file->private_data;
1582 struct acpi_video_bus *video = m->private;
4be44fcd 1583 char str[12] = { 0 };
27663c58 1584 unsigned long long opt, options;
1da177e4 1585
1da177e4 1586
52a2b11c 1587 if (!video || count >= sizeof(str))
d550d98d 1588 return -EINVAL;
1da177e4
LT
1589
1590 status = acpi_video_bus_POST_options(video, &options);
1591 if (!ACPI_SUCCESS(status))
d550d98d 1592 return -EINVAL;
1da177e4
LT
1593
1594 if (copy_from_user(str, buffer, count))
d550d98d 1595 return -EFAULT;
1da177e4
LT
1596
1597 str[count] = 0;
1598 opt = strtoul(str, NULL, 0);
1599 if (opt > 3)
d550d98d 1600 return -EFAULT;
1da177e4 1601
98fb8fe1 1602 /* just in case an OEM 'forgot' the motherboard... */
1da177e4
LT
1603 options |= 1;
1604
1605 if (options & (1ul << opt)) {
4be44fcd 1606 status = acpi_video_bus_set_POST(video, opt);
1da177e4 1607 if (!ACPI_SUCCESS(status))
d550d98d 1608 return -EFAULT;
1da177e4
LT
1609
1610 }
1611
d550d98d 1612 return count;
1da177e4
LT
1613}
1614
1615static ssize_t
4be44fcd
LB
1616acpi_video_bus_write_DOS(struct file *file,
1617 const char __user * buffer,
1618 size_t count, loff_t * data)
1da177e4 1619{
4be44fcd 1620 int status;
50dd0969
JE
1621 struct seq_file *m = file->private_data;
1622 struct acpi_video_bus *video = m->private;
4be44fcd
LB
1623 char str[12] = { 0 };
1624 unsigned long opt;
1da177e4 1625
1da177e4 1626
52a2b11c 1627 if (!video || count >= sizeof(str))
d550d98d 1628 return -EINVAL;
1da177e4
LT
1629
1630 if (copy_from_user(str, buffer, count))
d550d98d 1631 return -EFAULT;
1da177e4
LT
1632
1633 str[count] = 0;
1634 opt = strtoul(str, NULL, 0);
1635 if (opt > 7)
d550d98d 1636 return -EFAULT;
1da177e4 1637
4be44fcd 1638 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
1da177e4
LT
1639
1640 if (!ACPI_SUCCESS(status))
d550d98d 1641 return -EFAULT;
1da177e4 1642
d550d98d 1643 return count;
1da177e4
LT
1644}
1645
4be44fcd 1646static int acpi_video_bus_add_fs(struct acpi_device *device)
1da177e4 1647{
251cb0bc
DT
1648 struct acpi_video_bus *video = acpi_driver_data(device);
1649 struct proc_dir_entry *device_dir;
1650 struct proc_dir_entry *entry;
1da177e4 1651
251cb0bc
DT
1652 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1653 if (!device_dir)
1654 return -ENOMEM;
1da177e4 1655
1da177e4 1656 /* 'info' [R] */
e0066c4e 1657 entry = proc_create_data("info", S_IRUGO, device_dir,
cf7acfab
DL
1658 &acpi_video_bus_info_fops,
1659 acpi_driver_data(device));
1da177e4 1660 if (!entry)
251cb0bc 1661 goto err_remove_dir;
1da177e4
LT
1662
1663 /* 'ROM' [R] */
e0066c4e 1664 entry = proc_create_data("ROM", S_IRUGO, device_dir,
cf7acfab
DL
1665 &acpi_video_bus_ROM_fops,
1666 acpi_driver_data(device));
1da177e4 1667 if (!entry)
251cb0bc 1668 goto err_remove_info;
1da177e4
LT
1669
1670 /* 'POST_info' [R] */
e0066c4e 1671 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
cf7acfab
DL
1672 &acpi_video_bus_POST_info_fops,
1673 acpi_driver_data(device));
1da177e4 1674 if (!entry)
251cb0bc 1675 goto err_remove_rom;
1da177e4
LT
1676
1677 /* 'POST' [R/W] */
08acd4f8 1678 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
e0066c4e 1679 device_dir,
cf7acfab
DL
1680 &acpi_video_bus_POST_fops,
1681 acpi_driver_data(device));
1da177e4 1682 if (!entry)
251cb0bc 1683 goto err_remove_post_info;
1da177e4
LT
1684
1685 /* 'DOS' [R/W] */
08acd4f8 1686 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
e0066c4e 1687 device_dir,
cf7acfab
DL
1688 &acpi_video_bus_DOS_fops,
1689 acpi_driver_data(device));
1da177e4 1690 if (!entry)
251cb0bc 1691 goto err_remove_post;
1da177e4 1692
251cb0bc 1693 video->dir = acpi_device_dir(device) = device_dir;
d550d98d 1694 return 0;
251cb0bc
DT
1695
1696 err_remove_post:
1697 remove_proc_entry("POST", device_dir);
1698 err_remove_post_info:
1699 remove_proc_entry("POST_info", device_dir);
1700 err_remove_rom:
1701 remove_proc_entry("ROM", device_dir);
1702 err_remove_info:
1703 remove_proc_entry("info", device_dir);
1704 err_remove_dir:
1705 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1706 return -ENOMEM;
1da177e4
LT
1707}
1708
4be44fcd 1709static int acpi_video_bus_remove_fs(struct acpi_device *device)
1da177e4 1710{
251cb0bc
DT
1711 struct proc_dir_entry *device_dir = acpi_device_dir(device);
1712
1713 if (device_dir) {
1714 remove_proc_entry("info", device_dir);
1715 remove_proc_entry("ROM", device_dir);
1716 remove_proc_entry("POST_info", device_dir);
1717 remove_proc_entry("POST", device_dir);
1718 remove_proc_entry("DOS", device_dir);
4be44fcd 1719 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1da177e4
LT
1720 acpi_device_dir(device) = NULL;
1721 }
1722
d550d98d 1723 return 0;
1da177e4
LT
1724}
1725
1726/* --------------------------------------------------------------------------
1727 Driver Interface
1728 -------------------------------------------------------------------------- */
1729
1730/* device interface */
82cae999
RZ
1731static struct acpi_video_device_attrib*
1732acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1733{
78eed028
DT
1734 struct acpi_video_enumerated_device *ids;
1735 int i;
1736
1737 for (i = 0; i < video->attached_count; i++) {
1738 ids = &video->attached_array[i];
1739 if ((ids->value.int_val & 0xffff) == device_id)
1740 return &ids->value.attrib;
1741 }
82cae999 1742
82cae999
RZ
1743 return NULL;
1744}
1da177e4 1745
e92a7162
MG
1746static int
1747acpi_video_get_device_type(struct acpi_video_bus *video,
1748 unsigned long device_id)
1749{
1750 struct acpi_video_enumerated_device *ids;
1751 int i;
1752
1753 for (i = 0; i < video->attached_count; i++) {
1754 ids = &video->attached_array[i];
1755 if ((ids->value.int_val & 0xffff) == device_id)
1756 return ids->value.int_val;
1757 }
1758
1759 return 0;
1760}
1761
1da177e4 1762static int
4be44fcd
LB
1763acpi_video_bus_get_one_device(struct acpi_device *device,
1764 struct acpi_video_bus *video)
1da177e4 1765{
27663c58 1766 unsigned long long device_id;
e92a7162 1767 int status, device_type;
4be44fcd 1768 struct acpi_video_device *data;
82cae999 1769 struct acpi_video_device_attrib* attribute;
1da177e4
LT
1770
1771 if (!device || !video)
d550d98d 1772 return -EINVAL;
1da177e4 1773
4be44fcd
LB
1774 status =
1775 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1da177e4
LT
1776 if (ACPI_SUCCESS(status)) {
1777
36bcbec7 1778 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1da177e4 1779 if (!data)
d550d98d 1780 return -ENOMEM;
1da177e4 1781
1da177e4
LT
1782 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1783 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
db89b4f0 1784 device->driver_data = data;
1da177e4
LT
1785
1786 data->device_id = device_id;
1787 data->video = video;
1788 data->dev = device;
1789
82cae999
RZ
1790 attribute = acpi_video_get_device_attr(video, device_id);
1791
1792 if((attribute != NULL) && attribute->device_id_scheme) {
1793 switch (attribute->display_type) {
1794 case ACPI_VIDEO_DISPLAY_CRT:
1795 data->flags.crt = 1;
1796 break;
1797 case ACPI_VIDEO_DISPLAY_TV:
1798 data->flags.tvout = 1;
1799 break;
1800 case ACPI_VIDEO_DISPLAY_DVI:
1801 data->flags.dvi = 1;
1802 break;
1803 case ACPI_VIDEO_DISPLAY_LCD:
1804 data->flags.lcd = 1;
1805 break;
1806 default:
1807 data->flags.unknown = 1;
1808 break;
1809 }
1810 if(attribute->bios_can_detect)
1811 data->flags.bios = 1;
e92a7162
MG
1812 } else {
1813 /* Check for legacy IDs */
1814 device_type = acpi_video_get_device_type(video,
1815 device_id);
1816 /* Ignore bits 16 and 18-20 */
1817 switch (device_type & 0xffe2ffff) {
1818 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1819 data->flags.crt = 1;
1820 break;
1821 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1822 data->flags.lcd = 1;
1823 break;
1824 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1825 data->flags.tvout = 1;
1826 break;
1827 default:
1828 data->flags.unknown = 1;
1829 }
1830 }
4be44fcd 1831
1da177e4
LT
1832 acpi_video_device_bind(video, data);
1833 acpi_video_device_find_cap(data);
1834
90130268 1835 status = acpi_install_notify_handler(device->handle,
4be44fcd
LB
1836 ACPI_DEVICE_NOTIFY,
1837 acpi_video_device_notify,
1838 data);
1da177e4 1839 if (ACPI_FAILURE(status)) {
55ac9a01
LM
1840 printk(KERN_ERR PREFIX
1841 "Error installing notify handler\n");
973bf491
YL
1842 if(data->brightness)
1843 kfree(data->brightness->levels);
1844 kfree(data->brightness);
1845 kfree(data);
1846 return -ENODEV;
1da177e4
LT
1847 }
1848
bbac81f5 1849 mutex_lock(&video->device_list_lock);
1da177e4 1850 list_add_tail(&data->entry, &video->video_device_list);
bbac81f5 1851 mutex_unlock(&video->device_list_lock);
1da177e4
LT
1852
1853 acpi_video_device_add_fs(device);
1854
d550d98d 1855 return 0;
1da177e4
LT
1856 }
1857
d550d98d 1858 return -ENOENT;
1da177e4
LT
1859}
1860
1861/*
1862 * Arg:
1863 * video : video bus device
1864 *
1865 * Return:
1866 * none
1867 *
1868 * Enumerate the video device list of the video bus,
1869 * bind the ids with the corresponding video devices
1870 * under the video bus.
4be44fcd 1871 */
1da177e4 1872
4be44fcd 1873static void acpi_video_device_rebind(struct acpi_video_bus *video)
1da177e4 1874{
ff102ea9
DT
1875 struct acpi_video_device *dev;
1876
bbac81f5 1877 mutex_lock(&video->device_list_lock);
ff102ea9
DT
1878
1879 list_for_each_entry(dev, &video->video_device_list, entry)
4be44fcd 1880 acpi_video_device_bind(video, dev);
ff102ea9 1881
bbac81f5 1882 mutex_unlock(&video->device_list_lock);
1da177e4
LT
1883}
1884
1885/*
1886 * Arg:
1887 * video : video bus device
1888 * device : video output device under the video
1889 * bus
1890 *
1891 * Return:
1892 * none
1893 *
1894 * Bind the ids with the corresponding video devices
1895 * under the video bus.
4be44fcd 1896 */
1da177e4
LT
1897
1898static void
4be44fcd
LB
1899acpi_video_device_bind(struct acpi_video_bus *video,
1900 struct acpi_video_device *device)
1da177e4 1901{
78eed028 1902 struct acpi_video_enumerated_device *ids;
4be44fcd 1903 int i;
1da177e4 1904
78eed028
DT
1905 for (i = 0; i < video->attached_count; i++) {
1906 ids = &video->attached_array[i];
1907 if (device->device_id == (ids->value.int_val & 0xffff)) {
1908 ids->bind_info = device;
1da177e4
LT
1909 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1910 }
1911 }
1da177e4
LT
1912}
1913
1914/*
1915 * Arg:
1916 * video : video bus device
1917 *
1918 * Return:
1919 * < 0 : error
1920 *
1921 * Call _DOD to enumerate all devices attached to display adapter
1922 *
4be44fcd 1923 */
1da177e4
LT
1924
1925static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1926{
4be44fcd
LB
1927 int status;
1928 int count;
1929 int i;
78eed028 1930 struct acpi_video_enumerated_device *active_list;
4be44fcd
LB
1931 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1932 union acpi_object *dod = NULL;
1933 union acpi_object *obj;
1da177e4 1934
90130268 1935 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1da177e4 1936 if (!ACPI_SUCCESS(status)) {
a6fc6720 1937 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
d550d98d 1938 return status;
1da177e4
LT
1939 }
1940
50dd0969 1941 dod = buffer.pointer;
1da177e4 1942 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
a6fc6720 1943 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1da177e4
LT
1944 status = -EFAULT;
1945 goto out;
1946 }
1947
1948 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
4be44fcd 1949 dod->package.count));
1da177e4 1950
78eed028
DT
1951 active_list = kcalloc(1 + dod->package.count,
1952 sizeof(struct acpi_video_enumerated_device),
1953 GFP_KERNEL);
1954 if (!active_list) {
1da177e4
LT
1955 status = -ENOMEM;
1956 goto out;
1957 }
1958
1959 count = 0;
1960 for (i = 0; i < dod->package.count; i++) {
50dd0969 1961 obj = &dod->package.elements[i];
1da177e4
LT
1962
1963 if (obj->type != ACPI_TYPE_INTEGER) {
78eed028
DT
1964 printk(KERN_ERR PREFIX
1965 "Invalid _DOD data in element %d\n", i);
1966 continue;
1da177e4 1967 }
78eed028
DT
1968
1969 active_list[count].value.int_val = obj->integer.value;
1970 active_list[count].bind_info = NULL;
4be44fcd
LB
1971 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1972 (int)obj->integer.value));
1da177e4
LT
1973 count++;
1974 }
1da177e4 1975
6044ec88 1976 kfree(video->attached_array);
4be44fcd 1977
78eed028 1978 video->attached_array = active_list;
1da177e4 1979 video->attached_count = count;
78eed028
DT
1980
1981 out:
02438d87 1982 kfree(buffer.pointer);
d550d98d 1983 return status;
1da177e4
LT
1984}
1985
4be44fcd
LB
1986static int
1987acpi_video_get_next_level(struct acpi_video_device *device,
1988 u32 level_current, u32 event)
1da177e4 1989{
63f0edfc 1990 int min, max, min_above, max_below, i, l, delta = 255;
f4715189
TT
1991 max = max_below = 0;
1992 min = min_above = 255;
63f0edfc 1993 /* Find closest level to level_current */
0a3db1ce 1994 for (i = 2; i < device->brightness->count; i++) {
63f0edfc
AS
1995 l = device->brightness->levels[i];
1996 if (abs(l - level_current) < abs(delta)) {
1997 delta = l - level_current;
1998 if (!delta)
1999 break;
2000 }
2001 }
2002 /* Ajust level_current to closest available level */
2003 level_current += delta;
0a3db1ce 2004 for (i = 2; i < device->brightness->count; i++) {
f4715189
TT
2005 l = device->brightness->levels[i];
2006 if (l < min)
2007 min = l;
2008 if (l > max)
2009 max = l;
2010 if (l < min_above && l > level_current)
2011 min_above = l;
2012 if (l > max_below && l < level_current)
2013 max_below = l;
2014 }
2015
2016 switch (event) {
2017 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
2018 return (level_current < max) ? min_above : min;
2019 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
2020 return (level_current < max) ? min_above : max;
2021 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
2022 return (level_current > min) ? max_below : min;
2023 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
2024 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
2025 return 0;
2026 default:
2027 return level_current;
2028 }
1da177e4
LT
2029}
2030
c8890f90 2031static int
4be44fcd 2032acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1da177e4 2033{
27663c58 2034 unsigned long long level_current, level_next;
c8890f90
ZR
2035 int result = -EINVAL;
2036
28c32e99
ZR
2037 /* no warning message if acpi_backlight=vendor is used */
2038 if (!acpi_video_backlight_support())
2039 return 0;
2040
469778c1 2041 if (!device->brightness)
c8890f90
ZR
2042 goto out;
2043
2044 result = acpi_video_device_lcd_get_level_current(device,
70287db8 2045 &level_current, 0);
c8890f90
ZR
2046 if (result)
2047 goto out;
2048
1da177e4 2049 level_next = acpi_video_get_next_level(device, level_current, event);
c8890f90 2050
24450c7a 2051 result = acpi_video_device_lcd_set_level(device, level_next);
c8890f90 2052
36342742
MG
2053 if (!result)
2054 backlight_force_update(device->backlight,
2055 BACKLIGHT_UPDATE_HOTKEY);
2056
c8890f90
ZR
2057out:
2058 if (result)
2059 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
2060
2061 return result;
1da177e4
LT
2062}
2063
e92a7162
MG
2064int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
2065 void **edid)
2066{
2067 struct acpi_video_bus *video;
2068 struct acpi_video_device *video_device;
2069 union acpi_object *buffer = NULL;
2070 acpi_status status;
2071 int i, length;
2072
2073 if (!device || !acpi_driver_data(device))
2074 return -EINVAL;
2075
2076 video = acpi_driver_data(device);
2077
2078 for (i = 0; i < video->attached_count; i++) {
2079 video_device = video->attached_array[i].bind_info;
2080 length = 256;
2081
2082 if (!video_device)
2083 continue;
2084
2085 if (type) {
2086 switch (type) {
2087 case ACPI_VIDEO_DISPLAY_CRT:
2088 if (!video_device->flags.crt)
2089 continue;
2090 break;
2091 case ACPI_VIDEO_DISPLAY_TV:
2092 if (!video_device->flags.tvout)
2093 continue;
2094 break;
2095 case ACPI_VIDEO_DISPLAY_DVI:
2096 if (!video_device->flags.dvi)
2097 continue;
2098 break;
2099 case ACPI_VIDEO_DISPLAY_LCD:
2100 if (!video_device->flags.lcd)
2101 continue;
2102 break;
2103 }
2104 } else if (video_device->device_id != device_id) {
2105 continue;
2106 }
2107
2108 status = acpi_video_device_EDID(video_device, &buffer, length);
2109
2110 if (ACPI_FAILURE(status) || !buffer ||
2111 buffer->type != ACPI_TYPE_BUFFER) {
2112 length = 128;
2113 status = acpi_video_device_EDID(video_device, &buffer,
2114 length);
2115 if (ACPI_FAILURE(status) || !buffer ||
2116 buffer->type != ACPI_TYPE_BUFFER) {
2117 continue;
2118 }
2119 }
2120
2121 *edid = buffer->buffer.pointer;
2122 return length;
2123 }
2124
2125 return -ENODEV;
2126}
2127EXPORT_SYMBOL(acpi_video_get_edid);
2128
1da177e4 2129static int
4be44fcd
LB
2130acpi_video_bus_get_devices(struct acpi_video_bus *video,
2131 struct acpi_device *device)
1da177e4 2132{
4be44fcd 2133 int status = 0;
ff102ea9 2134 struct acpi_device *dev;
1da177e4
LT
2135
2136 acpi_video_device_enumerate(video);
2137
ff102ea9 2138 list_for_each_entry(dev, &device->children, node) {
1da177e4
LT
2139
2140 status = acpi_video_bus_get_one_device(dev, video);
2141 if (ACPI_FAILURE(status)) {
55ac9a01
LM
2142 printk(KERN_WARNING PREFIX
2143 "Cant attach device");
1da177e4
LT
2144 continue;
2145 }
1da177e4 2146 }
d550d98d 2147 return status;
1da177e4
LT
2148}
2149
4be44fcd 2150static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1da177e4 2151{
031ec77b 2152 acpi_status status;
1da177e4
LT
2153 struct acpi_video_bus *video;
2154
1da177e4
LT
2155
2156 if (!device || !device->video)
d550d98d 2157 return -ENOENT;
1da177e4
LT
2158
2159 video = device->video;
2160
1da177e4
LT
2161 acpi_video_device_remove_fs(device->dev);
2162
90130268 2163 status = acpi_remove_notify_handler(device->dev->handle,
4be44fcd
LB
2164 ACPI_DEVICE_NOTIFY,
2165 acpi_video_device_notify);
e29b3ee3
KP
2166 if (device->backlight) {
2167 sysfs_remove_link(&device->backlight->dev.kobj, "device");
2168 backlight_device_unregister(device->backlight);
2169 device->backlight = NULL;
2170 }
4a703a8f 2171 if (device->cooling_dev) {
702ed512
ZR
2172 sysfs_remove_link(&device->dev->dev.kobj,
2173 "thermal_cooling");
4a703a8f 2174 sysfs_remove_link(&device->cooling_dev->device.kobj,
702ed512 2175 "device");
4a703a8f
DT
2176 thermal_cooling_device_unregister(device->cooling_dev);
2177 device->cooling_dev = NULL;
702ed512 2178 }
23b0f015 2179 video_output_unregister(device->output_dev);
ff102ea9 2180
d550d98d 2181 return 0;
1da177e4
LT
2182}
2183
4be44fcd 2184static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1da177e4 2185{
4be44fcd 2186 int status;
ff102ea9 2187 struct acpi_video_device *dev, *next;
1da177e4 2188
bbac81f5 2189 mutex_lock(&video->device_list_lock);
1da177e4 2190
ff102ea9 2191 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1da177e4 2192
ff102ea9 2193 status = acpi_video_bus_put_one_device(dev);
4be44fcd
LB
2194 if (ACPI_FAILURE(status))
2195 printk(KERN_WARNING PREFIX
2196 "hhuuhhuu bug in acpi video driver.\n");
1da177e4 2197
ff102ea9
DT
2198 if (dev->brightness) {
2199 kfree(dev->brightness->levels);
2200 kfree(dev->brightness);
2201 }
2202 list_del(&dev->entry);
2203 kfree(dev);
1da177e4
LT
2204 }
2205
bbac81f5 2206 mutex_unlock(&video->device_list_lock);
ff102ea9 2207
d550d98d 2208 return 0;
1da177e4
LT
2209}
2210
2211/* acpi_video interface */
2212
4be44fcd 2213static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1da177e4 2214{
a21101c4 2215 return acpi_video_bus_DOS(video, 0, 0);
1da177e4
LT
2216}
2217
4be44fcd 2218static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1da177e4
LT
2219{
2220 return acpi_video_bus_DOS(video, 0, 1);
2221}
2222
7015558f 2223static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
1da177e4 2224{
7015558f 2225 struct acpi_video_bus *video = acpi_driver_data(device);
e9dab196 2226 struct input_dev *input;
17c452f9 2227 int keycode = 0;
e9dab196 2228
1da177e4 2229 if (!video)
d550d98d 2230 return;
1da177e4 2231
e9dab196 2232 input = video->input;
1da177e4
LT
2233
2234 switch (event) {
98fb8fe1 2235 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
1da177e4 2236 * most likely via hotkey. */
14e04fb3 2237 acpi_bus_generate_proc_event(device, event, 0);
e9dab196 2238 keycode = KEY_SWITCHVIDEOMODE;
1da177e4
LT
2239 break;
2240
98fb8fe1 2241 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
1da177e4
LT
2242 * connector. */
2243 acpi_video_device_enumerate(video);
2244 acpi_video_device_rebind(video);
14e04fb3 2245 acpi_bus_generate_proc_event(device, event, 0);
e9dab196 2246 keycode = KEY_SWITCHVIDEOMODE;
1da177e4
LT
2247 break;
2248
4be44fcd 2249 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
25c87f7f 2250 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
2251 keycode = KEY_SWITCHVIDEOMODE;
2252 break;
4be44fcd 2253 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
25c87f7f 2254 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
2255 keycode = KEY_VIDEO_NEXT;
2256 break;
4be44fcd 2257 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
14e04fb3 2258 acpi_bus_generate_proc_event(device, event, 0);
e9dab196 2259 keycode = KEY_VIDEO_PREV;
1da177e4
LT
2260 break;
2261
2262 default:
2263 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 2264 "Unsupported event [0x%x]\n", event));
1da177e4
LT
2265 break;
2266 }
2267
7761f638 2268 acpi_notifier_call_chain(device, event, 0);
17c452f9
MG
2269
2270 if (keycode) {
2271 input_report_key(input, keycode, 1);
2272 input_sync(input);
2273 input_report_key(input, keycode, 0);
2274 input_sync(input);
2275 }
e9dab196 2276
d550d98d 2277 return;
1da177e4
LT
2278}
2279
4be44fcd 2280static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1da177e4 2281{
50dd0969 2282 struct acpi_video_device *video_device = data;
4be44fcd 2283 struct acpi_device *device = NULL;
e9dab196
LY
2284 struct acpi_video_bus *bus;
2285 struct input_dev *input;
17c452f9 2286 int keycode = 0;
1da177e4 2287
1da177e4 2288 if (!video_device)
d550d98d 2289 return;
1da177e4 2290
e6afa0de 2291 device = video_device->dev;
e9dab196
LY
2292 bus = video_device->video;
2293 input = bus->input;
1da177e4
LT
2294
2295 switch (event) {
4be44fcd 2296 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
8a681a4d
ZR
2297 if (brightness_switch_enabled)
2298 acpi_video_switch_brightness(video_device, event);
14e04fb3 2299 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
2300 keycode = KEY_BRIGHTNESS_CYCLE;
2301 break;
4be44fcd 2302 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
8a681a4d
ZR
2303 if (brightness_switch_enabled)
2304 acpi_video_switch_brightness(video_device, event);
25c87f7f 2305 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
2306 keycode = KEY_BRIGHTNESSUP;
2307 break;
4be44fcd 2308 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
8a681a4d
ZR
2309 if (brightness_switch_enabled)
2310 acpi_video_switch_brightness(video_device, event);
25c87f7f 2311 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
2312 keycode = KEY_BRIGHTNESSDOWN;
2313 break;
4be44fcd 2314 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
8a681a4d
ZR
2315 if (brightness_switch_enabled)
2316 acpi_video_switch_brightness(video_device, event);
25c87f7f 2317 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
2318 keycode = KEY_BRIGHTNESS_ZERO;
2319 break;
4be44fcd 2320 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
8a681a4d
ZR
2321 if (brightness_switch_enabled)
2322 acpi_video_switch_brightness(video_device, event);
14e04fb3 2323 acpi_bus_generate_proc_event(device, event, 0);
e9dab196 2324 keycode = KEY_DISPLAY_OFF;
1da177e4
LT
2325 break;
2326 default:
2327 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 2328 "Unsupported event [0x%x]\n", event));
1da177e4
LT
2329 break;
2330 }
e9dab196 2331
7761f638 2332 acpi_notifier_call_chain(device, event, 0);
17c452f9
MG
2333
2334 if (keycode) {
2335 input_report_key(input, keycode, 1);
2336 input_sync(input);
2337 input_report_key(input, keycode, 0);
2338 input_sync(input);
2339 }
e9dab196 2340
d550d98d 2341 return;
1da177e4
LT
2342}
2343
ac7729da
RW
2344static int acpi_video_resume(struct notifier_block *nb,
2345 unsigned long val, void *ign)
863c1490
MG
2346{
2347 struct acpi_video_bus *video;
2348 struct acpi_video_device *video_device;
2349 int i;
2350
ac7729da
RW
2351 switch (val) {
2352 case PM_HIBERNATION_PREPARE:
2353 case PM_SUSPEND_PREPARE:
2354 case PM_RESTORE_PREPARE:
2355 return NOTIFY_DONE;
2356 }
863c1490 2357
ac7729da
RW
2358 video = container_of(nb, struct acpi_video_bus, pm_nb);
2359
2360 dev_info(&video->device->dev, "Restoring backlight state\n");
863c1490
MG
2361
2362 for (i = 0; i < video->attached_count; i++) {
2363 video_device = video->attached_array[i].bind_info;
2364 if (video_device && video_device->backlight)
2365 acpi_video_set_brightness(video_device->backlight);
2366 }
ac7729da
RW
2367
2368 return NOTIFY_OK;
863c1490
MG
2369}
2370
c504f8cb
ZR
2371static acpi_status
2372acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
2373 void **return_value)
2374{
2375 struct acpi_device *device = context;
2376 struct acpi_device *sibling;
2377 int result;
2378
2379 if (handle == device->handle)
2380 return AE_CTRL_TERMINATE;
2381
2382 result = acpi_bus_get_device(handle, &sibling);
2383 if (result)
2384 return AE_OK;
2385
2386 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
2387 return AE_ALREADY_EXISTS;
2388
2389 return AE_OK;
2390}
2391
ac7729da
RW
2392static int instance;
2393
4be44fcd 2394static int acpi_video_bus_add(struct acpi_device *device)
1da177e4 2395{
f51e8391 2396 struct acpi_video_bus *video;
e9dab196 2397 struct input_dev *input;
f51e8391 2398 int error;
c504f8cb
ZR
2399 acpi_status status;
2400
2401 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
2402 device->parent->handle, 1,
2403 acpi_video_bus_match, NULL,
2404 device, NULL);
2405 if (status == AE_ALREADY_EXISTS) {
2406 printk(KERN_WARNING FW_BUG
2407 "Duplicate ACPI video bus devices for the"
2408 " same VGA controller, please try module "
2409 "parameter \"video.allow_duplicates=1\""
2410 "if the current driver doesn't work.\n");
2411 if (!allow_duplicates)
2412 return -ENODEV;
2413 }
1da177e4 2414
36bcbec7 2415 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1da177e4 2416 if (!video)
d550d98d 2417 return -ENOMEM;
1da177e4 2418
e6d9da1d
ZR
2419 /* a hack to fix the duplicate name "VID" problem on T61 */
2420 if (!strcmp(device->pnp.bus_id, "VID")) {
2421 if (instance)
2422 device->pnp.bus_id[3] = '0' + instance;
2423 instance ++;
2424 }
f3b39f13
ZY
2425 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2426 if (!strcmp(device->pnp.bus_id, "VGA")) {
2427 if (instance)
2428 device->pnp.bus_id[3] = '0' + instance;
2429 instance++;
2430 }
e6d9da1d 2431
e6afa0de 2432 video->device = device;
1da177e4
LT
2433 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2434 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
db89b4f0 2435 device->driver_data = video;
1da177e4
LT
2436
2437 acpi_video_bus_find_cap(video);
f51e8391
DT
2438 error = acpi_video_bus_check(video);
2439 if (error)
2440 goto err_free_video;
1da177e4 2441
f51e8391
DT
2442 error = acpi_video_bus_add_fs(device);
2443 if (error)
2444 goto err_free_video;
1da177e4 2445
bbac81f5 2446 mutex_init(&video->device_list_lock);
1da177e4
LT
2447 INIT_LIST_HEAD(&video->video_device_list);
2448
2449 acpi_video_bus_get_devices(video, device);
2450 acpi_video_bus_start_devices(video);
2451
e9dab196 2452 video->input = input = input_allocate_device();
f51e8391
DT
2453 if (!input) {
2454 error = -ENOMEM;
7015558f 2455 goto err_stop_video;
f51e8391 2456 }
e9dab196
LY
2457
2458 snprintf(video->phys, sizeof(video->phys),
2459 "%s/video/input0", acpi_device_hid(video->device));
2460
2461 input->name = acpi_device_name(video->device);
2462 input->phys = video->phys;
2463 input->id.bustype = BUS_HOST;
2464 input->id.product = 0x06;
91c05c66 2465 input->dev.parent = &device->dev;
e9dab196
LY
2466 input->evbit[0] = BIT(EV_KEY);
2467 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2468 set_bit(KEY_VIDEO_NEXT, input->keybit);
2469 set_bit(KEY_VIDEO_PREV, input->keybit);
2470 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2471 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2472 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2473 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2474 set_bit(KEY_DISPLAY_OFF, input->keybit);
e9dab196 2475
f51e8391
DT
2476 error = input_register_device(input);
2477 if (error)
2478 goto err_free_input_dev;
e9dab196 2479
1da177e4 2480 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
4be44fcd
LB
2481 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2482 video->flags.multihead ? "yes" : "no",
2483 video->flags.rom ? "yes" : "no",
2484 video->flags.post ? "yes" : "no");
1da177e4 2485
ac7729da
RW
2486 video->pm_nb.notifier_call = acpi_video_resume;
2487 video->pm_nb.priority = 0;
2488 register_pm_notifier(&video->pm_nb);
2489
f51e8391
DT
2490 return 0;
2491
2492 err_free_input_dev:
2493 input_free_device(input);
f51e8391
DT
2494 err_stop_video:
2495 acpi_video_bus_stop_devices(video);
2496 acpi_video_bus_put_devices(video);
2497 kfree(video->attached_array);
2498 acpi_video_bus_remove_fs(device);
2499 err_free_video:
2500 kfree(video);
db89b4f0 2501 device->driver_data = NULL;
1da177e4 2502
f51e8391 2503 return error;
1da177e4
LT
2504}
2505
4be44fcd 2506static int acpi_video_bus_remove(struct acpi_device *device, int type)
1da177e4 2507{
4be44fcd 2508 struct acpi_video_bus *video = NULL;
1da177e4 2509
1da177e4
LT
2510
2511 if (!device || !acpi_driver_data(device))
d550d98d 2512 return -EINVAL;
1da177e4 2513
50dd0969 2514 video = acpi_driver_data(device);
1da177e4 2515
ac7729da
RW
2516 unregister_pm_notifier(&video->pm_nb);
2517
1da177e4 2518 acpi_video_bus_stop_devices(video);
1da177e4
LT
2519 acpi_video_bus_put_devices(video);
2520 acpi_video_bus_remove_fs(device);
2521
e9dab196 2522 input_unregister_device(video->input);
6044ec88 2523 kfree(video->attached_array);
1da177e4
LT
2524 kfree(video);
2525
d550d98d 2526 return 0;
1da177e4
LT
2527}
2528
74a365b3
MG
2529static int __init intel_opregion_present(void)
2530{
2531#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2532 struct pci_dev *dev = NULL;
2533 u32 address;
2534
2535 for_each_pci_dev(dev) {
2536 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2537 continue;
2538 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2539 continue;
2540 pci_read_config_dword(dev, 0xfc, &address);
2541 if (!address)
2542 continue;
2543 return 1;
2544 }
2545#endif
2546 return 0;
2547}
2548
2549int acpi_video_register(void)
1da177e4 2550{
4be44fcd 2551 int result = 0;
86e437f0
ZY
2552 if (register_count) {
2553 /*
2554 * if the function of acpi_video_register is already called,
2555 * don't register the acpi_vide_bus again and return no error.
2556 */
2557 return 0;
2558 }
1da177e4 2559
1da177e4
LT
2560 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2561 if (!acpi_video_dir)
d550d98d 2562 return -ENODEV;
1da177e4
LT
2563
2564 result = acpi_bus_register_driver(&acpi_video_bus);
2565 if (result < 0) {
2566 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
d550d98d 2567 return -ENODEV;
1da177e4
LT
2568 }
2569
86e437f0
ZY
2570 /*
2571 * When the acpi_video_bus is loaded successfully, increase
2572 * the counter reference.
2573 */
2574 register_count = 1;
2575
d550d98d 2576 return 0;
1da177e4 2577}
74a365b3
MG
2578EXPORT_SYMBOL(acpi_video_register);
2579
86e437f0
ZY
2580void acpi_video_unregister(void)
2581{
2582 if (!register_count) {
2583 /*
2584 * If the acpi video bus is already unloaded, don't
2585 * unload it again and return directly.
2586 */
2587 return;
2588 }
2589 acpi_bus_unregister_driver(&acpi_video_bus);
2590
2591 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2592
2593 register_count = 0;
2594
2595 return;
2596}
2597EXPORT_SYMBOL(acpi_video_unregister);
2598
74a365b3
MG
2599/*
2600 * This is kind of nasty. Hardware using Intel chipsets may require
2601 * the video opregion code to be run first in order to initialise
2602 * state before any ACPI video calls are made. To handle this we defer
2603 * registration of the video class until the opregion code has run.
2604 */
2605
2606static int __init acpi_video_init(void)
2607{
45cb50e6
ZR
2608 dmi_check_system(video_dmi_table);
2609
74a365b3
MG
2610 if (intel_opregion_present())
2611 return 0;
2612
2613 return acpi_video_register();
2614}
1da177e4 2615
86e437f0 2616static void __exit acpi_video_exit(void)
1da177e4 2617{
86e437f0 2618 acpi_video_unregister();
1da177e4 2619
d550d98d 2620 return;
1da177e4
LT
2621}
2622
2623module_init(acpi_video_init);
2624module_exit(acpi_video_exit);
This page took 0.849307 seconds and 5 git commands to generate.