hibernation: constify platform_hibernation_ops
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
e9dab196 33#include <linux/input.h>
2f3d000a 34#include <linux/backlight.h>
702ed512 35#include <linux/thermal.h>
23b0f015 36#include <linux/video_output.h>
935e5f29 37#include <linux/sort.h>
74a365b3
MG
38#include <linux/pci.h>
39#include <linux/pci_ids.h>
5a0e3ad6 40#include <linux/slab.h>
1da177e4 41#include <asm/uaccess.h>
eb27cae8 42#include <linux/dmi.h>
1da177e4
LT
43#include <acpi/acpi_bus.h>
44#include <acpi/acpi_drivers.h>
ac7729da 45#include <linux/suspend.h>
e92a7162 46#include <acpi/video.h>
1da177e4 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
1da177e4 67#define _COMPONENT ACPI_VIDEO_COMPONENT
f52fd66d 68ACPI_MODULE_NAME("video");
1da177e4 69
f52fd66d 70MODULE_AUTHOR("Bruno Ducrot");
7cda93e0 71MODULE_DESCRIPTION("ACPI Video Driver");
1da177e4
LT
72MODULE_LICENSE("GPL");
73
8a681a4d
ZR
74static int brightness_switch_enabled = 1;
75module_param(brightness_switch_enabled, bool, 0644);
76
c504f8cb
ZR
77/*
78 * By default, we don't allow duplicate ACPI video bus devices
79 * under the same VGA controller
80 */
81static int allow_duplicates;
82module_param(allow_duplicates, bool, 0644);
83
86e437f0 84static int register_count = 0;
4be44fcd
LB
85static int acpi_video_bus_add(struct acpi_device *device);
86static int acpi_video_bus_remove(struct acpi_device *device, int type);
7015558f 87static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
1da177e4 88
1ba90e3a
TR
89static const struct acpi_device_id video_device_ids[] = {
90 {ACPI_VIDEO_HID, 0},
91 {"", 0},
92};
93MODULE_DEVICE_TABLE(acpi, video_device_ids);
94
1da177e4 95static struct acpi_driver acpi_video_bus = {
c2b6705b 96 .name = "video",
1da177e4 97 .class = ACPI_VIDEO_CLASS,
1ba90e3a 98 .ids = video_device_ids,
1da177e4
LT
99 .ops = {
100 .add = acpi_video_bus_add,
101 .remove = acpi_video_bus_remove,
7015558f 102 .notify = acpi_video_bus_notify,
4be44fcd 103 },
1da177e4
LT
104};
105
106struct acpi_video_bus_flags {
4be44fcd
LB
107 u8 multihead:1; /* can switch video heads */
108 u8 rom:1; /* can retrieve a video rom */
109 u8 post:1; /* can configure the head to */
110 u8 reserved:5;
1da177e4
LT
111};
112
113struct acpi_video_bus_cap {
4be44fcd
LB
114 u8 _DOS:1; /*Enable/Disable output switching */
115 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
116 u8 _ROM:1; /*Get ROM Data */
117 u8 _GPD:1; /*Get POST Device */
118 u8 _SPD:1; /*Set POST Device */
119 u8 _VPO:1; /*Video POST Options */
120 u8 reserved:2;
1da177e4
LT
121};
122
4be44fcd
LB
123struct acpi_video_device_attrib {
124 u32 display_index:4; /* A zero-based instance of the Display */
98fb8fe1 125 u32 display_port_attachment:4; /*This field differentiates the display type */
4be44fcd 126 u32 display_type:4; /*Describe the specific type in use */
98fb8fe1 127 u32 vendor_specific:4; /*Chipset Vendor Specific */
4be44fcd
LB
128 u32 bios_can_detect:1; /*BIOS can detect the device */
129 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
130 the VGA device. */
131 u32 pipe_id:3; /*For VGA multiple-head devices. */
132 u32 reserved:10; /*Must be 0 */
133 u32 device_id_scheme:1; /*Device ID Scheme */
1da177e4
LT
134};
135
136struct acpi_video_enumerated_device {
137 union {
138 u32 int_val;
4be44fcd 139 struct acpi_video_device_attrib attrib;
1da177e4
LT
140 } value;
141 struct acpi_video_device *bind_info;
142};
143
144struct acpi_video_bus {
e6afa0de 145 struct acpi_device *device;
4be44fcd 146 u8 dos_setting;
1da177e4 147 struct acpi_video_enumerated_device *attached_array;
4be44fcd
LB
148 u8 attached_count;
149 struct acpi_video_bus_cap cap;
1da177e4 150 struct acpi_video_bus_flags flags;
4be44fcd 151 struct list_head video_device_list;
bbac81f5 152 struct mutex device_list_lock; /* protects video_device_list */
e9dab196
LY
153 struct input_dev *input;
154 char phys[32]; /* for input device */
ac7729da 155 struct notifier_block pm_nb;
1da177e4
LT
156};
157
158struct acpi_video_device_flags {
4be44fcd
LB
159 u8 crt:1;
160 u8 lcd:1;
161 u8 tvout:1;
82cae999 162 u8 dvi:1;
4be44fcd
LB
163 u8 bios:1;
164 u8 unknown:1;
82cae999 165 u8 reserved:2;
1da177e4
LT
166};
167
168struct acpi_video_device_cap {
4be44fcd
LB
169 u8 _ADR:1; /*Return the unique ID */
170 u8 _BCL:1; /*Query list of brightness control levels supported */
171 u8 _BCM:1; /*Set the brightness level */
2f3d000a 172 u8 _BQC:1; /* Get current brightness level */
c60d638e 173 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
4be44fcd
LB
174 u8 _DDC:1; /*Return the EDID for this device */
175 u8 _DCS:1; /*Return status of output device */
176 u8 _DGS:1; /*Query graphics state */
177 u8 _DSS:1; /*Device state set */
1da177e4
LT
178};
179
d32f6947
ZR
180struct acpi_video_brightness_flags {
181 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
d80fb99f 182 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
1a7c618a
ZR
183 u8 _BCL_use_index:1; /* levels in _BCL are index values */
184 u8 _BCM_use_index:1; /* input of _BCM is an index value */
185 u8 _BQC_use_index:1; /* _BQC returns an index value */
d32f6947
ZR
186};
187
1da177e4 188struct acpi_video_device_brightness {
4be44fcd
LB
189 int curr;
190 int count;
191 int *levels;
d32f6947 192 struct acpi_video_brightness_flags flags;
1da177e4
LT
193};
194
195struct acpi_video_device {
4be44fcd
LB
196 unsigned long device_id;
197 struct acpi_video_device_flags flags;
198 struct acpi_video_device_cap cap;
199 struct list_head entry;
200 struct acpi_video_bus *video;
201 struct acpi_device *dev;
1da177e4 202 struct acpi_video_device_brightness *brightness;
2f3d000a 203 struct backlight_device *backlight;
4a703a8f 204 struct thermal_cooling_device *cooling_dev;
23b0f015 205 struct output_device *output_dev;
1da177e4
LT
206};
207
b7171ae7 208static const char device_decode[][30] = {
1da177e4
LT
209 "motherboard VGA device",
210 "PCI VGA device",
211 "AGP VGA device",
212 "UNKNOWN",
213};
214
4be44fcd
LB
215static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
216static void acpi_video_device_rebind(struct acpi_video_bus *video);
217static void acpi_video_device_bind(struct acpi_video_bus *video,
218 struct acpi_video_device *device);
1da177e4 219static int acpi_video_device_enumerate(struct acpi_video_bus *video);
2f3d000a
YL
220static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
221 int level);
222static int acpi_video_device_lcd_get_level_current(
223 struct acpi_video_device *device,
70287db8 224 unsigned long long *level, int init);
4be44fcd
LB
225static int acpi_video_get_next_level(struct acpi_video_device *device,
226 u32 level_current, u32 event);
c8890f90 227static int acpi_video_switch_brightness(struct acpi_video_device *device,
4be44fcd 228 int event);
23b0f015 229static int acpi_video_device_get_state(struct acpi_video_device *device,
27663c58 230 unsigned long long *state);
23b0f015
LY
231static int acpi_video_output_get(struct output_device *od);
232static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
1da177e4 233
2f3d000a
YL
234/*backlight device sysfs support*/
235static int acpi_video_get_brightness(struct backlight_device *bd)
236{
27663c58 237 unsigned long long cur_level;
38531e6f 238 int i;
2f3d000a 239 struct acpi_video_device *vd =
655bfd7a 240 (struct acpi_video_device *)bl_get_data(bd);
c8890f90 241
70287db8 242 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, 0))
c8890f90 243 return -EINVAL;
38531e6f
MG
244 for (i = 2; i < vd->brightness->count; i++) {
245 if (vd->brightness->levels[i] == cur_level)
246 /* The first two entries are special - see page 575
247 of the ACPI spec 3.0 */
248 return i-2;
249 }
250 return 0;
2f3d000a
YL
251}
252
253static int acpi_video_set_brightness(struct backlight_device *bd)
254{
24450c7a 255 int request_level = bd->props.brightness + 2;
2f3d000a 256 struct acpi_video_device *vd =
655bfd7a 257 (struct acpi_video_device *)bl_get_data(bd);
24450c7a
ZR
258
259 return acpi_video_device_lcd_set_level(vd,
260 vd->brightness->levels[request_level]);
2f3d000a
YL
261}
262
599a52d1
RP
263static struct backlight_ops acpi_backlight_ops = {
264 .get_brightness = acpi_video_get_brightness,
265 .update_status = acpi_video_set_brightness,
266};
267
23b0f015
LY
268/*video output device sysfs support*/
269static int acpi_video_output_get(struct output_device *od)
270{
27663c58 271 unsigned long long state;
23b0f015 272 struct acpi_video_device *vd =
60043428 273 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
23b0f015
LY
274 acpi_video_device_get_state(vd, &state);
275 return (int)state;
276}
277
278static int acpi_video_output_set(struct output_device *od)
279{
280 unsigned long state = od->request_state;
281 struct acpi_video_device *vd=
60043428 282 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
23b0f015
LY
283 return acpi_video_device_set_state(vd, state);
284}
285
286static struct output_properties acpi_output_properties = {
287 .set_state = acpi_video_output_set,
288 .get_status = acpi_video_output_get,
289};
702ed512
ZR
290
291
292/* thermal cooling device callbacks */
4a703a8f 293static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
6503e5df 294 long *state)
702ed512 295{
4a703a8f 296 struct acpi_device *device = cooling_dev->devdata;
702ed512
ZR
297 struct acpi_video_device *video = acpi_driver_data(device);
298
6503e5df
MG
299 *state = video->brightness->count - 3;
300 return 0;
702ed512
ZR
301}
302
4a703a8f 303static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
6503e5df 304 long *state)
702ed512 305{
4a703a8f 306 struct acpi_device *device = cooling_dev->devdata;
702ed512 307 struct acpi_video_device *video = acpi_driver_data(device);
27663c58 308 unsigned long long level;
6503e5df 309 int offset;
702ed512 310
70287db8 311 if (acpi_video_device_lcd_get_level_current(video, &level, 0))
c8890f90 312 return -EINVAL;
6503e5df
MG
313 for (offset = 2; offset < video->brightness->count; offset++)
314 if (level == video->brightness->levels[offset]) {
315 *state = video->brightness->count - offset - 1;
316 return 0;
317 }
702ed512
ZR
318
319 return -EINVAL;
320}
321
322static int
4a703a8f 323video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
702ed512 324{
4a703a8f 325 struct acpi_device *device = cooling_dev->devdata;
702ed512
ZR
326 struct acpi_video_device *video = acpi_driver_data(device);
327 int level;
328
329 if ( state >= video->brightness->count - 2)
330 return -EINVAL;
331
332 state = video->brightness->count - state;
333 level = video->brightness->levels[state -1];
334 return acpi_video_device_lcd_set_level(video, level);
335}
336
337static struct thermal_cooling_device_ops video_cooling_ops = {
338 .get_max_state = video_get_max_state,
339 .get_cur_state = video_get_cur_state,
340 .set_cur_state = video_set_cur_state,
341};
342
1da177e4
LT
343/* --------------------------------------------------------------------------
344 Video Management
345 -------------------------------------------------------------------------- */
346
347/* device */
348
1da177e4 349static int
4be44fcd 350acpi_video_device_get_state(struct acpi_video_device *device,
27663c58 351 unsigned long long *state)
1da177e4 352{
4be44fcd 353 int status;
1da177e4 354
90130268 355 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
1da177e4 356
d550d98d 357 return status;
1da177e4
LT
358}
359
360static int
4be44fcd 361acpi_video_device_set_state(struct acpi_video_device *device, int state)
1da177e4 362{
4be44fcd
LB
363 int status;
364 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
365 struct acpi_object_list args = { 1, &arg0 };
27663c58 366 unsigned long long ret;
1da177e4 367
1da177e4
LT
368
369 arg0.integer.value = state;
90130268 370 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
1da177e4 371
d550d98d 372 return status;
1da177e4
LT
373}
374
375static int
4be44fcd
LB
376acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
377 union acpi_object **levels)
1da177e4 378{
4be44fcd
LB
379 int status;
380 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
381 union acpi_object *obj;
1da177e4 382
1da177e4
LT
383
384 *levels = NULL;
385
90130268 386 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
1da177e4 387 if (!ACPI_SUCCESS(status))
d550d98d 388 return status;
4be44fcd 389 obj = (union acpi_object *)buffer.pointer;
6665bda7 390 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6468463a 391 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
1da177e4
LT
392 status = -EFAULT;
393 goto err;
394 }
395
396 *levels = obj;
397
d550d98d 398 return 0;
1da177e4 399
4be44fcd 400 err:
6044ec88 401 kfree(buffer.pointer);
1da177e4 402
d550d98d 403 return status;
1da177e4
LT
404}
405
406static int
4be44fcd 407acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
1da177e4 408{
24450c7a 409 int status;
4be44fcd
LB
410 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
411 struct acpi_object_list args = { 1, &arg0 };
9e6dada9 412 int state;
1da177e4 413
1da177e4 414 arg0.integer.value = level;
1da177e4 415
24450c7a
ZR
416 status = acpi_evaluate_object(device->dev->handle, "_BCM",
417 &args, NULL);
418 if (ACPI_FAILURE(status)) {
419 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
420 return -EIO;
421 }
422
4500ca8e 423 device->brightness->curr = level;
9e6dada9 424 for (state = 2; state < device->brightness->count; state++)
24450c7a 425 if (level == device->brightness->levels[state]) {
1a7c618a
ZR
426 if (device->backlight)
427 device->backlight->props.brightness = state - 2;
24450c7a
ZR
428 return 0;
429 }
9e6dada9 430
24450c7a
ZR
431 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
432 return -EINVAL;
1da177e4
LT
433}
434
45cb50e6
ZR
435/*
436 * For some buggy _BQC methods, we need to add a constant value to
437 * the _BQC return value to get the actual current brightness level
438 */
439
440static int bqc_offset_aml_bug_workaround;
441static int __init video_set_bqc_offset(const struct dmi_system_id *d)
442{
443 bqc_offset_aml_bug_workaround = 9;
444 return 0;
445}
446
447static struct dmi_system_id video_dmi_table[] __initdata = {
448 /*
449 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
450 */
451 {
452 .callback = video_set_bqc_offset,
453 .ident = "Acer Aspire 5720",
454 .matches = {
455 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
456 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
457 },
458 },
5afc4abe
LB
459 {
460 .callback = video_set_bqc_offset,
461 .ident = "Acer Aspire 5710Z",
462 .matches = {
463 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
464 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
465 },
466 },
34ac272b
ZR
467 {
468 .callback = video_set_bqc_offset,
469 .ident = "eMachines E510",
470 .matches = {
471 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
472 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
473 },
474 },
93bcece2
ZR
475 {
476 .callback = video_set_bqc_offset,
477 .ident = "Acer Aspire 5315",
478 .matches = {
479 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
480 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
481 },
482 },
152a4e63
ZR
483 {
484 .callback = video_set_bqc_offset,
485 .ident = "Acer Aspire 7720",
486 .matches = {
487 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
488 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
489 },
490 },
45cb50e6
ZR
491 {}
492};
493
1da177e4 494static int
4be44fcd 495acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
70287db8 496 unsigned long long *level, int init)
1da177e4 497{
c8890f90 498 acpi_status status = AE_OK;
4e231fa4 499 int i;
c8890f90 500
c60d638e
ZR
501 if (device->cap._BQC || device->cap._BCQ) {
502 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
503
504 status = acpi_evaluate_integer(device->dev->handle, buf,
c8890f90
ZR
505 NULL, level);
506 if (ACPI_SUCCESS(status)) {
1a7c618a
ZR
507 if (device->brightness->flags._BQC_use_index) {
508 if (device->brightness->flags._BCL_reversed)
509 *level = device->brightness->count
510 - 3 - (*level);
511 *level = device->brightness->levels[*level + 2];
512
513 }
45cb50e6 514 *level += bqc_offset_aml_bug_workaround;
4e231fa4
VS
515 for (i = 2; i < device->brightness->count; i++)
516 if (device->brightness->levels[i] == *level) {
517 device->brightness->curr = *level;
518 return 0;
519 }
70287db8
MG
520 if (!init) {
521 /*
522 * BQC returned an invalid level.
523 * Stop using it.
524 */
525 ACPI_WARNING((AE_INFO,
526 "%s returned an invalid level",
527 buf));
528 device->cap._BQC = device->cap._BCQ = 0;
529 }
c8890f90
ZR
530 } else {
531 /* Fixme:
532 * should we return an error or ignore this failure?
533 * dev->brightness->curr is a cached value which stores
534 * the correct current backlight level in most cases.
535 * ACPI video backlight still works w/ buggy _BQC.
536 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
537 */
c60d638e
ZR
538 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
539 device->cap._BQC = device->cap._BCQ = 0;
c8890f90
ZR
540 }
541 }
542
4500ca8e 543 *level = device->brightness->curr;
c8890f90 544 return 0;
1da177e4
LT
545}
546
547static int
4be44fcd
LB
548acpi_video_device_EDID(struct acpi_video_device *device,
549 union acpi_object **edid, ssize_t length)
1da177e4 550{
4be44fcd
LB
551 int status;
552 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
553 union acpi_object *obj;
554 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
555 struct acpi_object_list args = { 1, &arg0 };
1da177e4 556
1da177e4
LT
557
558 *edid = NULL;
559
560 if (!device)
d550d98d 561 return -ENODEV;
1da177e4
LT
562 if (length == 128)
563 arg0.integer.value = 1;
564 else if (length == 256)
565 arg0.integer.value = 2;
566 else
d550d98d 567 return -EINVAL;
1da177e4 568
90130268 569 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
1da177e4 570 if (ACPI_FAILURE(status))
d550d98d 571 return -ENODEV;
1da177e4 572
50dd0969 573 obj = buffer.pointer;
1da177e4
LT
574
575 if (obj && obj->type == ACPI_TYPE_BUFFER)
576 *edid = obj;
577 else {
6468463a 578 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
1da177e4
LT
579 status = -EFAULT;
580 kfree(obj);
581 }
582
d550d98d 583 return status;
1da177e4
LT
584}
585
1da177e4
LT
586/* bus */
587
1da177e4
LT
588/*
589 * Arg:
590 * video : video bus device pointer
591 * bios_flag :
592 * 0. The system BIOS should NOT automatically switch(toggle)
593 * the active display output.
594 * 1. The system BIOS should automatically switch (toggle) the
98fb8fe1 595 * active display output. No switch event.
1da177e4
LT
596 * 2. The _DGS value should be locked.
597 * 3. The system BIOS should not automatically switch (toggle) the
598 * active display output, but instead generate the display switch
599 * event notify code.
600 * lcd_flag :
601 * 0. The system BIOS should automatically control the brightness level
98fb8fe1 602 * of the LCD when the power changes from AC to DC
1da177e4 603 * 1. The system BIOS should NOT automatically control the brightness
98fb8fe1 604 * level of the LCD when the power changes from AC to DC.
1da177e4
LT
605 * Return Value:
606 * -1 wrong arg.
607 */
608
609static int
4be44fcd 610acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
1da177e4 611{
439913ff 612 u64 status = 0;
4be44fcd
LB
613 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
614 struct acpi_object_list args = { 1, &arg0 };
1da177e4 615
1da177e4 616
4be44fcd 617 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
1da177e4
LT
618 status = -1;
619 goto Failed;
620 }
621 arg0.integer.value = (lcd_flag << 2) | bios_flag;
622 video->dos_setting = arg0.integer.value;
90130268 623 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
1da177e4 624
4be44fcd 625 Failed:
d550d98d 626 return status;
1da177e4
LT
627}
628
935e5f29
ZR
629/*
630 * Simple comparison function used to sort backlight levels.
631 */
632
633static int
634acpi_video_cmp_level(const void *a, const void *b)
635{
636 return *(int *)a - *(int *)b;
637}
638
1da177e4
LT
639/*
640 * Arg:
641 * device : video output device (LCD, CRT, ..)
642 *
643 * Return Value:
469778c1
JJ
644 * Maximum brightness level
645 *
646 * Allocate and initialize device->brightness.
647 */
648
649static int
650acpi_video_init_brightness(struct acpi_video_device *device)
651{
652 union acpi_object *obj = NULL;
d32f6947 653 int i, max_level = 0, count = 0, level_ac_battery = 0;
1a7c618a 654 unsigned long long level, level_old;
469778c1
JJ
655 union acpi_object *o;
656 struct acpi_video_device_brightness *br = NULL;
1a7c618a 657 int result = -EINVAL;
469778c1
JJ
658
659 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
660 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
661 "LCD brightness level\n"));
662 goto out;
663 }
664
665 if (obj->package.count < 2)
666 goto out;
667
668 br = kzalloc(sizeof(*br), GFP_KERNEL);
669 if (!br) {
670 printk(KERN_ERR "can't allocate memory\n");
1a7c618a 671 result = -ENOMEM;
469778c1
JJ
672 goto out;
673 }
674
d32f6947 675 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
469778c1 676 GFP_KERNEL);
1a7c618a
ZR
677 if (!br->levels) {
678 result = -ENOMEM;
469778c1 679 goto out_free;
1a7c618a 680 }
469778c1
JJ
681
682 for (i = 0; i < obj->package.count; i++) {
683 o = (union acpi_object *)&obj->package.elements[i];
684 if (o->type != ACPI_TYPE_INTEGER) {
685 printk(KERN_ERR PREFIX "Invalid data\n");
686 continue;
687 }
688 br->levels[count] = (u32) o->integer.value;
689
690 if (br->levels[count] > max_level)
691 max_level = br->levels[count];
692 count++;
693 }
694
d32f6947
ZR
695 /*
696 * some buggy BIOS don't export the levels
697 * when machine is on AC/Battery in _BCL package.
698 * In this case, the first two elements in _BCL packages
699 * are also supported brightness levels that OS should take care of.
700 */
90af2cf6
ZR
701 for (i = 2; i < count; i++) {
702 if (br->levels[i] == br->levels[0])
d32f6947 703 level_ac_battery++;
90af2cf6
ZR
704 if (br->levels[i] == br->levels[1])
705 level_ac_battery++;
706 }
d32f6947
ZR
707
708 if (level_ac_battery < 2) {
709 level_ac_battery = 2 - level_ac_battery;
710 br->flags._BCL_no_ac_battery_levels = 1;
711 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
712 br->levels[i] = br->levels[i - level_ac_battery];
713 count += level_ac_battery;
714 } else if (level_ac_battery > 2)
715 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
716
d80fb99f
ZR
717 /* Check if the _BCL package is in a reversed order */
718 if (max_level == br->levels[2]) {
719 br->flags._BCL_reversed = 1;
720 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
721 acpi_video_cmp_level, NULL);
722 } else if (max_level != br->levels[count - 1])
723 ACPI_ERROR((AE_INFO,
724 "Found unordered _BCL package\n"));
469778c1
JJ
725
726 br->count = count;
727 device->brightness = br;
1a7c618a
ZR
728
729 /* Check the input/output of _BQC/_BCL/_BCM */
730 if ((max_level < 100) && (max_level <= (count - 2)))
731 br->flags._BCL_use_index = 1;
732
733 /*
734 * _BCM is always consistent with _BCL,
735 * at least for all the laptops we have ever seen.
736 */
737 br->flags._BCM_use_index = br->flags._BCL_use_index;
738
739 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
90c53ca4 740 br->curr = level = max_level;
e047cca6
ZR
741
742 if (!device->cap._BQC)
743 goto set_level;
744
70287db8 745 result = acpi_video_device_lcd_get_level_current(device, &level_old, 1);
1a7c618a
ZR
746 if (result)
747 goto out_free_levels;
748
e047cca6
ZR
749 /*
750 * Set the level to maximum and check if _BQC uses indexed value
751 */
752 result = acpi_video_device_lcd_set_level(device, max_level);
1a7c618a
ZR
753 if (result)
754 goto out_free_levels;
755
70287db8 756 result = acpi_video_device_lcd_get_level_current(device, &level, 0);
1a7c618a
ZR
757 if (result)
758 goto out_free_levels;
759
e047cca6
ZR
760 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
761
90c53ca4
ZR
762 if (!br->flags._BQC_use_index) {
763 /*
764 * Set the backlight to the initial state.
765 * On some buggy laptops, _BQC returns an uninitialized value
766 * when invoked for the first time, i.e. level_old is invalid.
767 * set the backlight to max_level in this case
768 */
769 for (i = 2; i < br->count; i++)
770 if (level_old == br->levels[i])
771 level = level_old;
e047cca6 772 goto set_level;
90c53ca4 773 }
e047cca6
ZR
774
775 if (br->flags._BCL_reversed)
776 level_old = (br->count - 1) - level_old;
90c53ca4 777 level = br->levels[level_old];
e047cca6
ZR
778
779set_level:
90c53ca4 780 result = acpi_video_device_lcd_set_level(device, level);
e047cca6
ZR
781 if (result)
782 goto out_free_levels;
1a7c618a 783
d32f6947
ZR
784 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
785 "found %d brightness levels\n", count - 2));
469778c1 786 kfree(obj);
1a7c618a 787 return result;
469778c1
JJ
788
789out_free_levels:
790 kfree(br->levels);
791out_free:
792 kfree(br);
793out:
794 device->brightness = NULL;
795 kfree(obj);
1a7c618a 796 return result;
469778c1
JJ
797}
798
799/*
800 * Arg:
801 * device : video output device (LCD, CRT, ..)
802 *
803 * Return Value:
1da177e4
LT
804 * None
805 *
98fb8fe1 806 * Find out all required AML methods defined under the output
1da177e4
LT
807 * device.
808 */
809
4be44fcd 810static void acpi_video_device_find_cap(struct acpi_video_device *device)
1da177e4 811{
1da177e4 812 acpi_handle h_dummy1;
1da177e4 813
90130268 814 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
1da177e4
LT
815 device->cap._ADR = 1;
816 }
90130268 817 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
4be44fcd 818 device->cap._BCL = 1;
1da177e4 819 }
90130268 820 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
4be44fcd 821 device->cap._BCM = 1;
1da177e4 822 }
2f3d000a
YL
823 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
824 device->cap._BQC = 1;
c60d638e
ZR
825 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
826 &h_dummy1))) {
827 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
828 device->cap._BCQ = 1;
829 }
830
90130268 831 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
4be44fcd 832 device->cap._DDC = 1;
1da177e4 833 }
90130268 834 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
1da177e4
LT
835 device->cap._DCS = 1;
836 }
90130268 837 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
1da177e4
LT
838 device->cap._DGS = 1;
839 }
90130268 840 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
1da177e4
LT
841 device->cap._DSS = 1;
842 }
843
1a7c618a 844 if (acpi_video_backlight_support()) {
a19a6ee6 845 struct backlight_properties props;
702ed512 846 int result;
2f3d000a
YL
847 static int count = 0;
848 char *name;
1a7c618a
ZR
849
850 result = acpi_video_init_brightness(device);
851 if (result)
852 return;
aeb834d9 853 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
2f3d000a
YL
854 if (!name)
855 return;
aeb834d9 856 count++;
2f3d000a 857
a19a6ee6
MG
858 memset(&props, 0, sizeof(struct backlight_properties));
859 props.max_brightness = device->brightness->count - 3;
860 device->backlight = backlight_device_register(name, NULL, device,
861 &acpi_backlight_ops,
862 &props);
2f3d000a 863 kfree(name);
e01ce79b
ZR
864 if (IS_ERR(device->backlight))
865 return;
702ed512 866
ac7729da
RW
867 /*
868 * Save current brightness level in case we have to restore it
869 * before acpi_video_device_lcd_set_level() is called next time.
870 */
871 device->backlight->props.brightness =
872 acpi_video_get_brightness(device->backlight);
702ed512 873
056c308d
ZR
874 result = sysfs_create_link(&device->backlight->dev.kobj,
875 &device->dev->dev.kobj, "device");
876 if (result)
877 printk(KERN_ERR PREFIX "Create sysfs link\n");
878
4a703a8f 879 device->cooling_dev = thermal_cooling_device_register("LCD",
702ed512 880 device->dev, &video_cooling_ops);
4a703a8f 881 if (IS_ERR(device->cooling_dev)) {
4b4fe3b6 882 /*
4a703a8f 883 * Set cooling_dev to NULL so we don't crash trying to
4b4fe3b6
DT
884 * free it.
885 * Also, why the hell we are returning early and
886 * not attempt to register video output if cooling
887 * device registration failed?
888 * -- dtor
889 */
4a703a8f 890 device->cooling_dev = NULL;
43ff39f2 891 return;
4b4fe3b6 892 }
43ff39f2 893
fc3a8828 894 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
4a703a8f 895 device->cooling_dev->id);
9030062f 896 result = sysfs_create_link(&device->dev->dev.kobj,
4a703a8f 897 &device->cooling_dev->device.kobj,
9030062f
JL
898 "thermal_cooling");
899 if (result)
900 printk(KERN_ERR PREFIX "Create sysfs link\n");
4a703a8f 901 result = sysfs_create_link(&device->cooling_dev->device.kobj,
9030062f
JL
902 &device->dev->dev.kobj, "device");
903 if (result)
904 printk(KERN_ERR PREFIX "Create sysfs link\n");
905
2f3d000a 906 }
c3d6de69
TR
907
908 if (acpi_video_display_switch_support()) {
909
910 if (device->cap._DCS && device->cap._DSS) {
911 static int count;
912 char *name;
aeb834d9 913 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
c3d6de69
TR
914 if (!name)
915 return;
aeb834d9 916 count++;
c3d6de69
TR
917 device->output_dev = video_output_register(name,
918 NULL, device, &acpi_output_properties);
919 kfree(name);
920 }
23b0f015 921 }
1da177e4
LT
922}
923
924/*
925 * Arg:
926 * device : video output device (VGA)
927 *
928 * Return Value:
929 * None
930 *
98fb8fe1 931 * Find out all required AML methods defined under the video bus device.
1da177e4
LT
932 */
933
4be44fcd 934static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
1da177e4 935{
4be44fcd 936 acpi_handle h_dummy1;
1da177e4 937
90130268 938 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
1da177e4
LT
939 video->cap._DOS = 1;
940 }
90130268 941 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
1da177e4
LT
942 video->cap._DOD = 1;
943 }
90130268 944 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
1da177e4
LT
945 video->cap._ROM = 1;
946 }
90130268 947 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
1da177e4
LT
948 video->cap._GPD = 1;
949 }
90130268 950 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
1da177e4
LT
951 video->cap._SPD = 1;
952 }
90130268 953 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
1da177e4
LT
954 video->cap._VPO = 1;
955 }
956}
957
958/*
959 * Check whether the video bus device has required AML method to
960 * support the desired features
961 */
962
4be44fcd 963static int acpi_video_bus_check(struct acpi_video_bus *video)
1da177e4 964{
4be44fcd 965 acpi_status status = -ENOENT;
1e4cffe7 966 struct pci_dev *dev;
1da177e4
LT
967
968 if (!video)
d550d98d 969 return -EINVAL;
1da177e4 970
1e4cffe7 971 dev = acpi_get_pci_dev(video->device->handle);
22c13f9d
TR
972 if (!dev)
973 return -ENODEV;
1e4cffe7 974 pci_dev_put(dev);
22c13f9d 975
1da177e4
LT
976 /* Since there is no HID, CID and so on for VGA driver, we have
977 * to check well known required nodes.
978 */
979
98fb8fe1 980 /* Does this device support video switching? */
3a1151e3
SB
981 if (video->cap._DOS || video->cap._DOD) {
982 if (!video->cap._DOS) {
983 printk(KERN_WARNING FW_BUG
984 "ACPI(%s) defines _DOD but not _DOS\n",
985 acpi_device_bid(video->device));
986 }
1da177e4
LT
987 video->flags.multihead = 1;
988 status = 0;
989 }
990
98fb8fe1 991 /* Does this device support retrieving a video ROM? */
4be44fcd 992 if (video->cap._ROM) {
1da177e4
LT
993 video->flags.rom = 1;
994 status = 0;
995 }
996
98fb8fe1 997 /* Does this device support configuring which video device to POST? */
4be44fcd 998 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
1da177e4
LT
999 video->flags.post = 1;
1000 status = 0;
1001 }
1002
d550d98d 1003 return status;
1da177e4
LT
1004}
1005
1da177e4
LT
1006/* --------------------------------------------------------------------------
1007 Driver Interface
1008 -------------------------------------------------------------------------- */
1009
1010/* device interface */
82cae999
RZ
1011static struct acpi_video_device_attrib*
1012acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1013{
78eed028
DT
1014 struct acpi_video_enumerated_device *ids;
1015 int i;
1016
1017 for (i = 0; i < video->attached_count; i++) {
1018 ids = &video->attached_array[i];
1019 if ((ids->value.int_val & 0xffff) == device_id)
1020 return &ids->value.attrib;
1021 }
82cae999 1022
82cae999
RZ
1023 return NULL;
1024}
1da177e4 1025
e92a7162
MG
1026static int
1027acpi_video_get_device_type(struct acpi_video_bus *video,
1028 unsigned long device_id)
1029{
1030 struct acpi_video_enumerated_device *ids;
1031 int i;
1032
1033 for (i = 0; i < video->attached_count; i++) {
1034 ids = &video->attached_array[i];
1035 if ((ids->value.int_val & 0xffff) == device_id)
1036 return ids->value.int_val;
1037 }
1038
1039 return 0;
1040}
1041
1da177e4 1042static int
4be44fcd
LB
1043acpi_video_bus_get_one_device(struct acpi_device *device,
1044 struct acpi_video_bus *video)
1da177e4 1045{
27663c58 1046 unsigned long long device_id;
e92a7162 1047 int status, device_type;
4be44fcd 1048 struct acpi_video_device *data;
82cae999 1049 struct acpi_video_device_attrib* attribute;
1da177e4
LT
1050
1051 if (!device || !video)
d550d98d 1052 return -EINVAL;
1da177e4 1053
4be44fcd
LB
1054 status =
1055 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1da177e4
LT
1056 if (ACPI_SUCCESS(status)) {
1057
36bcbec7 1058 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1da177e4 1059 if (!data)
d550d98d 1060 return -ENOMEM;
1da177e4 1061
1da177e4
LT
1062 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1063 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
db89b4f0 1064 device->driver_data = data;
1da177e4
LT
1065
1066 data->device_id = device_id;
1067 data->video = video;
1068 data->dev = device;
1069
82cae999
RZ
1070 attribute = acpi_video_get_device_attr(video, device_id);
1071
1072 if((attribute != NULL) && attribute->device_id_scheme) {
1073 switch (attribute->display_type) {
1074 case ACPI_VIDEO_DISPLAY_CRT:
1075 data->flags.crt = 1;
1076 break;
1077 case ACPI_VIDEO_DISPLAY_TV:
1078 data->flags.tvout = 1;
1079 break;
1080 case ACPI_VIDEO_DISPLAY_DVI:
1081 data->flags.dvi = 1;
1082 break;
1083 case ACPI_VIDEO_DISPLAY_LCD:
1084 data->flags.lcd = 1;
1085 break;
1086 default:
1087 data->flags.unknown = 1;
1088 break;
1089 }
1090 if(attribute->bios_can_detect)
1091 data->flags.bios = 1;
e92a7162
MG
1092 } else {
1093 /* Check for legacy IDs */
1094 device_type = acpi_video_get_device_type(video,
1095 device_id);
1096 /* Ignore bits 16 and 18-20 */
1097 switch (device_type & 0xffe2ffff) {
1098 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1099 data->flags.crt = 1;
1100 break;
1101 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1102 data->flags.lcd = 1;
1103 break;
1104 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1105 data->flags.tvout = 1;
1106 break;
1107 default:
1108 data->flags.unknown = 1;
1109 }
1110 }
4be44fcd 1111
1da177e4
LT
1112 acpi_video_device_bind(video, data);
1113 acpi_video_device_find_cap(data);
1114
90130268 1115 status = acpi_install_notify_handler(device->handle,
4be44fcd
LB
1116 ACPI_DEVICE_NOTIFY,
1117 acpi_video_device_notify,
1118 data);
1da177e4 1119 if (ACPI_FAILURE(status)) {
55ac9a01
LM
1120 printk(KERN_ERR PREFIX
1121 "Error installing notify handler\n");
973bf491
YL
1122 if(data->brightness)
1123 kfree(data->brightness->levels);
1124 kfree(data->brightness);
1125 kfree(data);
1126 return -ENODEV;
1da177e4
LT
1127 }
1128
bbac81f5 1129 mutex_lock(&video->device_list_lock);
1da177e4 1130 list_add_tail(&data->entry, &video->video_device_list);
bbac81f5 1131 mutex_unlock(&video->device_list_lock);
1da177e4 1132
d550d98d 1133 return 0;
1da177e4
LT
1134 }
1135
d550d98d 1136 return -ENOENT;
1da177e4
LT
1137}
1138
1139/*
1140 * Arg:
1141 * video : video bus device
1142 *
1143 * Return:
1144 * none
1145 *
1146 * Enumerate the video device list of the video bus,
1147 * bind the ids with the corresponding video devices
1148 * under the video bus.
4be44fcd 1149 */
1da177e4 1150
4be44fcd 1151static void acpi_video_device_rebind(struct acpi_video_bus *video)
1da177e4 1152{
ff102ea9
DT
1153 struct acpi_video_device *dev;
1154
bbac81f5 1155 mutex_lock(&video->device_list_lock);
ff102ea9
DT
1156
1157 list_for_each_entry(dev, &video->video_device_list, entry)
4be44fcd 1158 acpi_video_device_bind(video, dev);
ff102ea9 1159
bbac81f5 1160 mutex_unlock(&video->device_list_lock);
1da177e4
LT
1161}
1162
1163/*
1164 * Arg:
1165 * video : video bus device
1166 * device : video output device under the video
1167 * bus
1168 *
1169 * Return:
1170 * none
1171 *
1172 * Bind the ids with the corresponding video devices
1173 * under the video bus.
4be44fcd 1174 */
1da177e4
LT
1175
1176static void
4be44fcd
LB
1177acpi_video_device_bind(struct acpi_video_bus *video,
1178 struct acpi_video_device *device)
1da177e4 1179{
78eed028 1180 struct acpi_video_enumerated_device *ids;
4be44fcd 1181 int i;
1da177e4 1182
78eed028
DT
1183 for (i = 0; i < video->attached_count; i++) {
1184 ids = &video->attached_array[i];
1185 if (device->device_id == (ids->value.int_val & 0xffff)) {
1186 ids->bind_info = device;
1da177e4
LT
1187 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1188 }
1189 }
1da177e4
LT
1190}
1191
1192/*
1193 * Arg:
1194 * video : video bus device
1195 *
1196 * Return:
1197 * < 0 : error
1198 *
1199 * Call _DOD to enumerate all devices attached to display adapter
1200 *
4be44fcd 1201 */
1da177e4
LT
1202
1203static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1204{
4be44fcd
LB
1205 int status;
1206 int count;
1207 int i;
78eed028 1208 struct acpi_video_enumerated_device *active_list;
4be44fcd
LB
1209 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1210 union acpi_object *dod = NULL;
1211 union acpi_object *obj;
1da177e4 1212
90130268 1213 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1da177e4 1214 if (!ACPI_SUCCESS(status)) {
a6fc6720 1215 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
d550d98d 1216 return status;
1da177e4
LT
1217 }
1218
50dd0969 1219 dod = buffer.pointer;
1da177e4 1220 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
a6fc6720 1221 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1da177e4
LT
1222 status = -EFAULT;
1223 goto out;
1224 }
1225
1226 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
4be44fcd 1227 dod->package.count));
1da177e4 1228
78eed028
DT
1229 active_list = kcalloc(1 + dod->package.count,
1230 sizeof(struct acpi_video_enumerated_device),
1231 GFP_KERNEL);
1232 if (!active_list) {
1da177e4
LT
1233 status = -ENOMEM;
1234 goto out;
1235 }
1236
1237 count = 0;
1238 for (i = 0; i < dod->package.count; i++) {
50dd0969 1239 obj = &dod->package.elements[i];
1da177e4
LT
1240
1241 if (obj->type != ACPI_TYPE_INTEGER) {
78eed028
DT
1242 printk(KERN_ERR PREFIX
1243 "Invalid _DOD data in element %d\n", i);
1244 continue;
1da177e4 1245 }
78eed028
DT
1246
1247 active_list[count].value.int_val = obj->integer.value;
1248 active_list[count].bind_info = NULL;
4be44fcd
LB
1249 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1250 (int)obj->integer.value));
1da177e4
LT
1251 count++;
1252 }
1da177e4 1253
6044ec88 1254 kfree(video->attached_array);
4be44fcd 1255
78eed028 1256 video->attached_array = active_list;
1da177e4 1257 video->attached_count = count;
78eed028
DT
1258
1259 out:
02438d87 1260 kfree(buffer.pointer);
d550d98d 1261 return status;
1da177e4
LT
1262}
1263
4be44fcd
LB
1264static int
1265acpi_video_get_next_level(struct acpi_video_device *device,
1266 u32 level_current, u32 event)
1da177e4 1267{
63f0edfc 1268 int min, max, min_above, max_below, i, l, delta = 255;
f4715189
TT
1269 max = max_below = 0;
1270 min = min_above = 255;
63f0edfc 1271 /* Find closest level to level_current */
0a3db1ce 1272 for (i = 2; i < device->brightness->count; i++) {
63f0edfc
AS
1273 l = device->brightness->levels[i];
1274 if (abs(l - level_current) < abs(delta)) {
1275 delta = l - level_current;
1276 if (!delta)
1277 break;
1278 }
1279 }
1280 /* Ajust level_current to closest available level */
1281 level_current += delta;
0a3db1ce 1282 for (i = 2; i < device->brightness->count; i++) {
f4715189
TT
1283 l = device->brightness->levels[i];
1284 if (l < min)
1285 min = l;
1286 if (l > max)
1287 max = l;
1288 if (l < min_above && l > level_current)
1289 min_above = l;
1290 if (l > max_below && l < level_current)
1291 max_below = l;
1292 }
1293
1294 switch (event) {
1295 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1296 return (level_current < max) ? min_above : min;
1297 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1298 return (level_current < max) ? min_above : max;
1299 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1300 return (level_current > min) ? max_below : min;
1301 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1302 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1303 return 0;
1304 default:
1305 return level_current;
1306 }
1da177e4
LT
1307}
1308
c8890f90 1309static int
4be44fcd 1310acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1da177e4 1311{
27663c58 1312 unsigned long long level_current, level_next;
c8890f90
ZR
1313 int result = -EINVAL;
1314
28c32e99
ZR
1315 /* no warning message if acpi_backlight=vendor is used */
1316 if (!acpi_video_backlight_support())
1317 return 0;
1318
469778c1 1319 if (!device->brightness)
c8890f90
ZR
1320 goto out;
1321
1322 result = acpi_video_device_lcd_get_level_current(device,
70287db8 1323 &level_current, 0);
c8890f90
ZR
1324 if (result)
1325 goto out;
1326
1da177e4 1327 level_next = acpi_video_get_next_level(device, level_current, event);
c8890f90 1328
24450c7a 1329 result = acpi_video_device_lcd_set_level(device, level_next);
c8890f90 1330
36342742
MG
1331 if (!result)
1332 backlight_force_update(device->backlight,
1333 BACKLIGHT_UPDATE_HOTKEY);
1334
c8890f90
ZR
1335out:
1336 if (result)
1337 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1338
1339 return result;
1da177e4
LT
1340}
1341
e92a7162
MG
1342int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1343 void **edid)
1344{
1345 struct acpi_video_bus *video;
1346 struct acpi_video_device *video_device;
1347 union acpi_object *buffer = NULL;
1348 acpi_status status;
1349 int i, length;
1350
1351 if (!device || !acpi_driver_data(device))
1352 return -EINVAL;
1353
1354 video = acpi_driver_data(device);
1355
1356 for (i = 0; i < video->attached_count; i++) {
1357 video_device = video->attached_array[i].bind_info;
1358 length = 256;
1359
1360 if (!video_device)
1361 continue;
1362
1363 if (type) {
1364 switch (type) {
1365 case ACPI_VIDEO_DISPLAY_CRT:
1366 if (!video_device->flags.crt)
1367 continue;
1368 break;
1369 case ACPI_VIDEO_DISPLAY_TV:
1370 if (!video_device->flags.tvout)
1371 continue;
1372 break;
1373 case ACPI_VIDEO_DISPLAY_DVI:
1374 if (!video_device->flags.dvi)
1375 continue;
1376 break;
1377 case ACPI_VIDEO_DISPLAY_LCD:
1378 if (!video_device->flags.lcd)
1379 continue;
1380 break;
1381 }
1382 } else if (video_device->device_id != device_id) {
1383 continue;
1384 }
1385
1386 status = acpi_video_device_EDID(video_device, &buffer, length);
1387
1388 if (ACPI_FAILURE(status) || !buffer ||
1389 buffer->type != ACPI_TYPE_BUFFER) {
1390 length = 128;
1391 status = acpi_video_device_EDID(video_device, &buffer,
1392 length);
1393 if (ACPI_FAILURE(status) || !buffer ||
1394 buffer->type != ACPI_TYPE_BUFFER) {
1395 continue;
1396 }
1397 }
1398
1399 *edid = buffer->buffer.pointer;
1400 return length;
1401 }
1402
1403 return -ENODEV;
1404}
1405EXPORT_SYMBOL(acpi_video_get_edid);
1406
1da177e4 1407static int
4be44fcd
LB
1408acpi_video_bus_get_devices(struct acpi_video_bus *video,
1409 struct acpi_device *device)
1da177e4 1410{
4be44fcd 1411 int status = 0;
ff102ea9 1412 struct acpi_device *dev;
1da177e4
LT
1413
1414 acpi_video_device_enumerate(video);
1415
ff102ea9 1416 list_for_each_entry(dev, &device->children, node) {
1da177e4
LT
1417
1418 status = acpi_video_bus_get_one_device(dev, video);
1419 if (ACPI_FAILURE(status)) {
55ac9a01 1420 printk(KERN_WARNING PREFIX
cfa806f0 1421 "Cant attach device\n");
1da177e4
LT
1422 continue;
1423 }
1da177e4 1424 }
d550d98d 1425 return status;
1da177e4
LT
1426}
1427
4be44fcd 1428static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1da177e4 1429{
031ec77b 1430 acpi_status status;
1da177e4
LT
1431
1432 if (!device || !device->video)
d550d98d 1433 return -ENOENT;
1da177e4 1434
90130268 1435 status = acpi_remove_notify_handler(device->dev->handle,
4be44fcd
LB
1436 ACPI_DEVICE_NOTIFY,
1437 acpi_video_device_notify);
cfa806f0
AK
1438 if (ACPI_FAILURE(status)) {
1439 printk(KERN_WARNING PREFIX
1440 "Cant remove video notify handler\n");
1441 }
e29b3ee3
KP
1442 if (device->backlight) {
1443 sysfs_remove_link(&device->backlight->dev.kobj, "device");
1444 backlight_device_unregister(device->backlight);
1445 device->backlight = NULL;
1446 }
4a703a8f 1447 if (device->cooling_dev) {
702ed512
ZR
1448 sysfs_remove_link(&device->dev->dev.kobj,
1449 "thermal_cooling");
4a703a8f 1450 sysfs_remove_link(&device->cooling_dev->device.kobj,
702ed512 1451 "device");
4a703a8f
DT
1452 thermal_cooling_device_unregister(device->cooling_dev);
1453 device->cooling_dev = NULL;
702ed512 1454 }
23b0f015 1455 video_output_unregister(device->output_dev);
ff102ea9 1456
d550d98d 1457 return 0;
1da177e4
LT
1458}
1459
4be44fcd 1460static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1da177e4 1461{
4be44fcd 1462 int status;
ff102ea9 1463 struct acpi_video_device *dev, *next;
1da177e4 1464
bbac81f5 1465 mutex_lock(&video->device_list_lock);
1da177e4 1466
ff102ea9 1467 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1da177e4 1468
ff102ea9 1469 status = acpi_video_bus_put_one_device(dev);
4be44fcd
LB
1470 if (ACPI_FAILURE(status))
1471 printk(KERN_WARNING PREFIX
1472 "hhuuhhuu bug in acpi video driver.\n");
1da177e4 1473
ff102ea9
DT
1474 if (dev->brightness) {
1475 kfree(dev->brightness->levels);
1476 kfree(dev->brightness);
1477 }
1478 list_del(&dev->entry);
1479 kfree(dev);
1da177e4
LT
1480 }
1481
bbac81f5 1482 mutex_unlock(&video->device_list_lock);
ff102ea9 1483
d550d98d 1484 return 0;
1da177e4
LT
1485}
1486
1487/* acpi_video interface */
1488
4be44fcd 1489static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1da177e4 1490{
a21101c4 1491 return acpi_video_bus_DOS(video, 0, 0);
1da177e4
LT
1492}
1493
4be44fcd 1494static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1da177e4
LT
1495{
1496 return acpi_video_bus_DOS(video, 0, 1);
1497}
1498
7015558f 1499static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
1da177e4 1500{
7015558f 1501 struct acpi_video_bus *video = acpi_driver_data(device);
e9dab196 1502 struct input_dev *input;
17c452f9 1503 int keycode = 0;
e9dab196 1504
1da177e4 1505 if (!video)
d550d98d 1506 return;
1da177e4 1507
e9dab196 1508 input = video->input;
1da177e4
LT
1509
1510 switch (event) {
98fb8fe1 1511 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
1da177e4 1512 * most likely via hotkey. */
14e04fb3 1513 acpi_bus_generate_proc_event(device, event, 0);
e9dab196 1514 keycode = KEY_SWITCHVIDEOMODE;
1da177e4
LT
1515 break;
1516
98fb8fe1 1517 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
1da177e4
LT
1518 * connector. */
1519 acpi_video_device_enumerate(video);
1520 acpi_video_device_rebind(video);
14e04fb3 1521 acpi_bus_generate_proc_event(device, event, 0);
e9dab196 1522 keycode = KEY_SWITCHVIDEOMODE;
1da177e4
LT
1523 break;
1524
4be44fcd 1525 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
25c87f7f 1526 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
1527 keycode = KEY_SWITCHVIDEOMODE;
1528 break;
4be44fcd 1529 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
25c87f7f 1530 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
1531 keycode = KEY_VIDEO_NEXT;
1532 break;
4be44fcd 1533 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
14e04fb3 1534 acpi_bus_generate_proc_event(device, event, 0);
e9dab196 1535 keycode = KEY_VIDEO_PREV;
1da177e4
LT
1536 break;
1537
1538 default:
1539 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 1540 "Unsupported event [0x%x]\n", event));
1da177e4
LT
1541 break;
1542 }
1543
7761f638 1544 acpi_notifier_call_chain(device, event, 0);
17c452f9
MG
1545
1546 if (keycode) {
1547 input_report_key(input, keycode, 1);
1548 input_sync(input);
1549 input_report_key(input, keycode, 0);
1550 input_sync(input);
1551 }
e9dab196 1552
d550d98d 1553 return;
1da177e4
LT
1554}
1555
4be44fcd 1556static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1da177e4 1557{
50dd0969 1558 struct acpi_video_device *video_device = data;
4be44fcd 1559 struct acpi_device *device = NULL;
e9dab196
LY
1560 struct acpi_video_bus *bus;
1561 struct input_dev *input;
17c452f9 1562 int keycode = 0;
1da177e4 1563
1da177e4 1564 if (!video_device)
d550d98d 1565 return;
1da177e4 1566
e6afa0de 1567 device = video_device->dev;
e9dab196
LY
1568 bus = video_device->video;
1569 input = bus->input;
1da177e4
LT
1570
1571 switch (event) {
4be44fcd 1572 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
8a681a4d
ZR
1573 if (brightness_switch_enabled)
1574 acpi_video_switch_brightness(video_device, event);
14e04fb3 1575 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
1576 keycode = KEY_BRIGHTNESS_CYCLE;
1577 break;
4be44fcd 1578 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
8a681a4d
ZR
1579 if (brightness_switch_enabled)
1580 acpi_video_switch_brightness(video_device, event);
25c87f7f 1581 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
1582 keycode = KEY_BRIGHTNESSUP;
1583 break;
4be44fcd 1584 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
8a681a4d
ZR
1585 if (brightness_switch_enabled)
1586 acpi_video_switch_brightness(video_device, event);
25c87f7f 1587 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
1588 keycode = KEY_BRIGHTNESSDOWN;
1589 break;
4be44fcd 1590 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
8a681a4d
ZR
1591 if (brightness_switch_enabled)
1592 acpi_video_switch_brightness(video_device, event);
25c87f7f 1593 acpi_bus_generate_proc_event(device, event, 0);
e9dab196
LY
1594 keycode = KEY_BRIGHTNESS_ZERO;
1595 break;
4be44fcd 1596 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
8a681a4d
ZR
1597 if (brightness_switch_enabled)
1598 acpi_video_switch_brightness(video_device, event);
14e04fb3 1599 acpi_bus_generate_proc_event(device, event, 0);
e9dab196 1600 keycode = KEY_DISPLAY_OFF;
1da177e4
LT
1601 break;
1602 default:
1603 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 1604 "Unsupported event [0x%x]\n", event));
1da177e4
LT
1605 break;
1606 }
e9dab196 1607
7761f638 1608 acpi_notifier_call_chain(device, event, 0);
17c452f9
MG
1609
1610 if (keycode) {
1611 input_report_key(input, keycode, 1);
1612 input_sync(input);
1613 input_report_key(input, keycode, 0);
1614 input_sync(input);
1615 }
e9dab196 1616
d550d98d 1617 return;
1da177e4
LT
1618}
1619
ac7729da
RW
1620static int acpi_video_resume(struct notifier_block *nb,
1621 unsigned long val, void *ign)
863c1490
MG
1622{
1623 struct acpi_video_bus *video;
1624 struct acpi_video_device *video_device;
1625 int i;
1626
ac7729da
RW
1627 switch (val) {
1628 case PM_HIBERNATION_PREPARE:
1629 case PM_SUSPEND_PREPARE:
1630 case PM_RESTORE_PREPARE:
1631 return NOTIFY_DONE;
1632 }
863c1490 1633
ac7729da
RW
1634 video = container_of(nb, struct acpi_video_bus, pm_nb);
1635
1636 dev_info(&video->device->dev, "Restoring backlight state\n");
863c1490
MG
1637
1638 for (i = 0; i < video->attached_count; i++) {
1639 video_device = video->attached_array[i].bind_info;
1640 if (video_device && video_device->backlight)
1641 acpi_video_set_brightness(video_device->backlight);
1642 }
ac7729da
RW
1643
1644 return NOTIFY_OK;
863c1490
MG
1645}
1646
c504f8cb
ZR
1647static acpi_status
1648acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1649 void **return_value)
1650{
1651 struct acpi_device *device = context;
1652 struct acpi_device *sibling;
1653 int result;
1654
1655 if (handle == device->handle)
1656 return AE_CTRL_TERMINATE;
1657
1658 result = acpi_bus_get_device(handle, &sibling);
1659 if (result)
1660 return AE_OK;
1661
1662 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1663 return AE_ALREADY_EXISTS;
1664
1665 return AE_OK;
1666}
1667
ac7729da
RW
1668static int instance;
1669
4be44fcd 1670static int acpi_video_bus_add(struct acpi_device *device)
1da177e4 1671{
f51e8391 1672 struct acpi_video_bus *video;
e9dab196 1673 struct input_dev *input;
f51e8391 1674 int error;
c504f8cb
ZR
1675 acpi_status status;
1676
1677 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1678 device->parent->handle, 1,
1679 acpi_video_bus_match, NULL,
1680 device, NULL);
1681 if (status == AE_ALREADY_EXISTS) {
1682 printk(KERN_WARNING FW_BUG
1683 "Duplicate ACPI video bus devices for the"
1684 " same VGA controller, please try module "
1685 "parameter \"video.allow_duplicates=1\""
1686 "if the current driver doesn't work.\n");
1687 if (!allow_duplicates)
1688 return -ENODEV;
1689 }
1da177e4 1690
36bcbec7 1691 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1da177e4 1692 if (!video)
d550d98d 1693 return -ENOMEM;
1da177e4 1694
e6d9da1d
ZR
1695 /* a hack to fix the duplicate name "VID" problem on T61 */
1696 if (!strcmp(device->pnp.bus_id, "VID")) {
1697 if (instance)
1698 device->pnp.bus_id[3] = '0' + instance;
1699 instance ++;
1700 }
f3b39f13
ZY
1701 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1702 if (!strcmp(device->pnp.bus_id, "VGA")) {
1703 if (instance)
1704 device->pnp.bus_id[3] = '0' + instance;
1705 instance++;
1706 }
e6d9da1d 1707
e6afa0de 1708 video->device = device;
1da177e4
LT
1709 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1710 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
db89b4f0 1711 device->driver_data = video;
1da177e4
LT
1712
1713 acpi_video_bus_find_cap(video);
f51e8391
DT
1714 error = acpi_video_bus_check(video);
1715 if (error)
1716 goto err_free_video;
1da177e4 1717
bbac81f5 1718 mutex_init(&video->device_list_lock);
1da177e4
LT
1719 INIT_LIST_HEAD(&video->video_device_list);
1720
1721 acpi_video_bus_get_devices(video, device);
1722 acpi_video_bus_start_devices(video);
1723
e9dab196 1724 video->input = input = input_allocate_device();
f51e8391
DT
1725 if (!input) {
1726 error = -ENOMEM;
7015558f 1727 goto err_stop_video;
f51e8391 1728 }
e9dab196
LY
1729
1730 snprintf(video->phys, sizeof(video->phys),
1731 "%s/video/input0", acpi_device_hid(video->device));
1732
1733 input->name = acpi_device_name(video->device);
1734 input->phys = video->phys;
1735 input->id.bustype = BUS_HOST;
1736 input->id.product = 0x06;
91c05c66 1737 input->dev.parent = &device->dev;
e9dab196
LY
1738 input->evbit[0] = BIT(EV_KEY);
1739 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1740 set_bit(KEY_VIDEO_NEXT, input->keybit);
1741 set_bit(KEY_VIDEO_PREV, input->keybit);
1742 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1743 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1744 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1745 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1746 set_bit(KEY_DISPLAY_OFF, input->keybit);
e9dab196 1747
f51e8391
DT
1748 error = input_register_device(input);
1749 if (error)
1750 goto err_free_input_dev;
e9dab196 1751
1da177e4 1752 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
4be44fcd
LB
1753 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1754 video->flags.multihead ? "yes" : "no",
1755 video->flags.rom ? "yes" : "no",
1756 video->flags.post ? "yes" : "no");
1da177e4 1757
ac7729da
RW
1758 video->pm_nb.notifier_call = acpi_video_resume;
1759 video->pm_nb.priority = 0;
1760 register_pm_notifier(&video->pm_nb);
1761
f51e8391
DT
1762 return 0;
1763
1764 err_free_input_dev:
1765 input_free_device(input);
f51e8391
DT
1766 err_stop_video:
1767 acpi_video_bus_stop_devices(video);
1768 acpi_video_bus_put_devices(video);
1769 kfree(video->attached_array);
f51e8391
DT
1770 err_free_video:
1771 kfree(video);
db89b4f0 1772 device->driver_data = NULL;
1da177e4 1773
f51e8391 1774 return error;
1da177e4
LT
1775}
1776
4be44fcd 1777static int acpi_video_bus_remove(struct acpi_device *device, int type)
1da177e4 1778{
4be44fcd 1779 struct acpi_video_bus *video = NULL;
1da177e4 1780
1da177e4
LT
1781
1782 if (!device || !acpi_driver_data(device))
d550d98d 1783 return -EINVAL;
1da177e4 1784
50dd0969 1785 video = acpi_driver_data(device);
1da177e4 1786
ac7729da
RW
1787 unregister_pm_notifier(&video->pm_nb);
1788
1da177e4 1789 acpi_video_bus_stop_devices(video);
1da177e4 1790 acpi_video_bus_put_devices(video);
1da177e4 1791
e9dab196 1792 input_unregister_device(video->input);
6044ec88 1793 kfree(video->attached_array);
1da177e4
LT
1794 kfree(video);
1795
d550d98d 1796 return 0;
1da177e4
LT
1797}
1798
74a365b3
MG
1799static int __init intel_opregion_present(void)
1800{
1801#if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
1802 struct pci_dev *dev = NULL;
1803 u32 address;
1804
1805 for_each_pci_dev(dev) {
1806 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1807 continue;
1808 if (dev->vendor != PCI_VENDOR_ID_INTEL)
1809 continue;
1810 pci_read_config_dword(dev, 0xfc, &address);
1811 if (!address)
1812 continue;
1813 return 1;
1814 }
1815#endif
1816 return 0;
1817}
1818
1819int acpi_video_register(void)
1da177e4 1820{
4be44fcd 1821 int result = 0;
86e437f0
ZY
1822 if (register_count) {
1823 /*
1824 * if the function of acpi_video_register is already called,
1825 * don't register the acpi_vide_bus again and return no error.
1826 */
1827 return 0;
1828 }
1da177e4 1829
1da177e4 1830 result = acpi_bus_register_driver(&acpi_video_bus);
39fe394d 1831 if (result < 0)
d550d98d 1832 return -ENODEV;
1da177e4 1833
86e437f0
ZY
1834 /*
1835 * When the acpi_video_bus is loaded successfully, increase
1836 * the counter reference.
1837 */
1838 register_count = 1;
1839
d550d98d 1840 return 0;
1da177e4 1841}
74a365b3
MG
1842EXPORT_SYMBOL(acpi_video_register);
1843
86e437f0
ZY
1844void acpi_video_unregister(void)
1845{
1846 if (!register_count) {
1847 /*
1848 * If the acpi video bus is already unloaded, don't
1849 * unload it again and return directly.
1850 */
1851 return;
1852 }
1853 acpi_bus_unregister_driver(&acpi_video_bus);
1854
86e437f0
ZY
1855 register_count = 0;
1856
1857 return;
1858}
1859EXPORT_SYMBOL(acpi_video_unregister);
1860
74a365b3
MG
1861/*
1862 * This is kind of nasty. Hardware using Intel chipsets may require
1863 * the video opregion code to be run first in order to initialise
1864 * state before any ACPI video calls are made. To handle this we defer
1865 * registration of the video class until the opregion code has run.
1866 */
1867
1868static int __init acpi_video_init(void)
1869{
45cb50e6
ZR
1870 dmi_check_system(video_dmi_table);
1871
74a365b3
MG
1872 if (intel_opregion_present())
1873 return 0;
1874
1875 return acpi_video_register();
1876}
1da177e4 1877
86e437f0 1878static void __exit acpi_video_exit(void)
1da177e4 1879{
86e437f0 1880 acpi_video_unregister();
1da177e4 1881
d550d98d 1882 return;
1da177e4
LT
1883}
1884
1885module_init(acpi_video_init);
1886module_exit(acpi_video_exit);