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