]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/acpi/acpi_video.c
KVM: arm64: vgic-v3: Log which GICv3 system registers are trapped
[mirror_ubuntu-zesty-kernel.git] / drivers / acpi / acpi_video.c
CommitLineData
1da177e4 1/*
21fcb34e 2 * video.c - ACPI Video Driver
1da177e4
LT
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 *
1da177e4
LT
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/types.h>
27#include <linux/list.h>
bbac81f5 28#include <linux/mutex.h>
e9dab196 29#include <linux/input.h>
2f3d000a 30#include <linux/backlight.h>
702ed512 31#include <linux/thermal.h>
935e5f29 32#include <linux/sort.h>
74a365b3
MG
33#include <linux/pci.h>
34#include <linux/pci_ids.h>
5a0e3ad6 35#include <linux/slab.h>
eb27cae8 36#include <linux/dmi.h>
ac7729da 37#include <linux/suspend.h>
8b48463f 38#include <linux/acpi.h>
e92a7162 39#include <acpi/video.h>
7c0f6ba6 40#include <linux/uaccess.h>
1da177e4 41
14ca7a47 42#define PREFIX "ACPI: "
8c5bd7ad 43
1da177e4
LT
44#define ACPI_VIDEO_BUS_NAME "Video Bus"
45#define ACPI_VIDEO_DEVICE_NAME "Video Device"
1da177e4 46
2f3d000a 47#define MAX_NAME_LEN 20
1da177e4 48
1da177e4 49#define _COMPONENT ACPI_VIDEO_COMPONENT
f52fd66d 50ACPI_MODULE_NAME("video");
1da177e4 51
f52fd66d 52MODULE_AUTHOR("Bruno Ducrot");
7cda93e0 53MODULE_DESCRIPTION("ACPI Video Driver");
1da177e4
LT
54MODULE_LICENSE("GPL");
55
2843768b 56static bool brightness_switch_enabled = 1;
8a681a4d
ZR
57module_param(brightness_switch_enabled, bool, 0644);
58
c504f8cb
ZR
59/*
60 * By default, we don't allow duplicate ACPI video bus devices
61 * under the same VGA controller
62 */
90ab5ee9 63static bool allow_duplicates;
c504f8cb
ZR
64module_param(allow_duplicates, bool, 0644);
65
654a182d
HG
66static int disable_backlight_sysfs_if = -1;
67module_param(disable_backlight_sysfs_if, int, 0444);
68
05bc59a0
HG
69#define REPORT_OUTPUT_KEY_EVENTS 0x01
70#define REPORT_BRIGHTNESS_KEY_EVENTS 0x02
71static int report_key_events = -1;
72module_param(report_key_events, int, 0644);
73MODULE_PARM_DESC(report_key_events,
74 "0: none, 1: output changes, 2: brightness changes, 3: all");
75
e50b9be1
AL
76static bool device_id_scheme = false;
77module_param(device_id_scheme, bool, 0444);
78
79static bool only_lcd = false;
80module_param(only_lcd, bool, 0444);
81
970530cd
HG
82static int register_count;
83static DEFINE_MUTEX(register_count_mutex);
14e93553
HG
84static DEFINE_MUTEX(video_list_lock);
85static LIST_HEAD(video_bus_head);
4be44fcd 86static int acpi_video_bus_add(struct acpi_device *device);
51fac838 87static int acpi_video_bus_remove(struct acpi_device *device);
7015558f 88static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
93a291df 89void acpi_video_detect_exit(void);
1da177e4 90
1ba90e3a
TR
91static const struct acpi_device_id video_device_ids[] = {
92 {ACPI_VIDEO_HID, 0},
93 {"", 0},
94};
95MODULE_DEVICE_TABLE(acpi, video_device_ids);
96
1da177e4 97static struct acpi_driver acpi_video_bus = {
c2b6705b 98 .name = "video",
1da177e4 99 .class = ACPI_VIDEO_CLASS,
1ba90e3a 100 .ids = video_device_ids,
1da177e4
LT
101 .ops = {
102 .add = acpi_video_bus_add,
103 .remove = acpi_video_bus_remove,
7015558f 104 .notify = acpi_video_bus_notify,
4be44fcd 105 },
1da177e4
LT
106};
107
108struct acpi_video_bus_flags {
4be44fcd
LB
109 u8 multihead:1; /* can switch video heads */
110 u8 rom:1; /* can retrieve a video rom */
111 u8 post:1; /* can configure the head to */
112 u8 reserved:5;
1da177e4
LT
113};
114
115struct acpi_video_bus_cap {
21fcb34e
FC
116 u8 _DOS:1; /* Enable/Disable output switching */
117 u8 _DOD:1; /* Enumerate all devices attached to display adapter */
118 u8 _ROM:1; /* Get ROM Data */
119 u8 _GPD:1; /* Get POST Device */
120 u8 _SPD:1; /* Set POST Device */
121 u8 _VPO:1; /* Video POST Options */
4be44fcd 122 u8 reserved:2;
1da177e4
LT
123};
124
4be44fcd
LB
125struct acpi_video_device_attrib {
126 u32 display_index:4; /* A zero-based instance of the Display */
21fcb34e
FC
127 u32 display_port_attachment:4; /* This field differentiates the display type */
128 u32 display_type:4; /* Describe the specific type in use */
129 u32 vendor_specific:4; /* Chipset Vendor Specific */
130 u32 bios_can_detect:1; /* BIOS can detect the device */
131 u32 depend_on_vga:1; /* Non-VGA output device whose power is related to
4be44fcd 132 the VGA device. */
21fcb34e
FC
133 u32 pipe_id:3; /* For VGA multiple-head devices. */
134 u32 reserved:10; /* Must be 0 */
135 u32 device_id_scheme:1; /* Device ID Scheme */
1da177e4
LT
136};
137
138struct acpi_video_enumerated_device {
139 union {
140 u32 int_val;
4be44fcd 141 struct acpi_video_device_attrib attrib;
1da177e4
LT
142 } value;
143 struct acpi_video_device *bind_info;
144};
145
146struct acpi_video_bus {
e6afa0de 147 struct acpi_device *device;
99678ed7 148 bool backlight_registered;
4be44fcd 149 u8 dos_setting;
1da177e4 150 struct acpi_video_enumerated_device *attached_array;
4be44fcd 151 u8 attached_count;
b4df4636 152 u8 child_count;
4be44fcd 153 struct acpi_video_bus_cap cap;
1da177e4 154 struct acpi_video_bus_flags flags;
4be44fcd 155 struct list_head video_device_list;
bbac81f5 156 struct mutex device_list_lock; /* protects video_device_list */
67b662e1 157 struct list_head entry;
e9dab196
LY
158 struct input_dev *input;
159 char phys[32]; /* for input device */
ac7729da 160 struct notifier_block pm_nb;
1da177e4
LT
161};
162
163struct acpi_video_device_flags {
4be44fcd
LB
164 u8 crt:1;
165 u8 lcd:1;
166 u8 tvout:1;
82cae999 167 u8 dvi:1;
4be44fcd
LB
168 u8 bios:1;
169 u8 unknown:1;
91e13aa3
AL
170 u8 notify:1;
171 u8 reserved:1;
1da177e4
LT
172};
173
174struct acpi_video_device_cap {
21fcb34e
FC
175 u8 _ADR:1; /* Return the unique ID */
176 u8 _BCL:1; /* Query list of brightness control levels supported */
177 u8 _BCM:1; /* Set the brightness level */
2f3d000a 178 u8 _BQC:1; /* Get current brightness level */
c60d638e 179 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
21fcb34e 180 u8 _DDC:1; /* Return the EDID for this device */
1da177e4
LT
181};
182
1da177e4 183struct acpi_video_device {
4be44fcd
LB
184 unsigned long device_id;
185 struct acpi_video_device_flags flags;
186 struct acpi_video_device_cap cap;
187 struct list_head entry;
8ab58e8e
LT
188 struct delayed_work switch_brightness_work;
189 int switch_brightness_event;
4be44fcd
LB
190 struct acpi_video_bus *video;
191 struct acpi_device *dev;
1da177e4 192 struct acpi_video_device_brightness *brightness;
2f3d000a 193 struct backlight_device *backlight;
4a703a8f 194 struct thermal_cooling_device *cooling_dev;
1da177e4
LT
195};
196
4be44fcd
LB
197static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
198static void acpi_video_device_rebind(struct acpi_video_bus *video);
199static void acpi_video_device_bind(struct acpi_video_bus *video,
200 struct acpi_video_device *device);
1da177e4 201static int acpi_video_device_enumerate(struct acpi_video_bus *video);
2f3d000a
YL
202static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
203 int level);
204static int acpi_video_device_lcd_get_level_current(
205 struct acpi_video_device *device,
a89803df 206 unsigned long long *level, bool raw);
4be44fcd
LB
207static int acpi_video_get_next_level(struct acpi_video_device *device,
208 u32 level_current, u32 event);
8ab58e8e 209static void acpi_video_switch_brightness(struct work_struct *work);
1da177e4 210
21fcb34e 211/* backlight device sysfs support */
2f3d000a
YL
212static int acpi_video_get_brightness(struct backlight_device *bd)
213{
27663c58 214 unsigned long long cur_level;
38531e6f 215 int i;
7c364e79 216 struct acpi_video_device *vd = bl_get_data(bd);
c8890f90 217
a89803df 218 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
c8890f90 219 return -EINVAL;
38531e6f
MG
220 for (i = 2; i < vd->brightness->count; i++) {
221 if (vd->brightness->levels[i] == cur_level)
21fcb34e
FC
222 /*
223 * The first two entries are special - see page 575
224 * of the ACPI spec 3.0
225 */
915ea7e4 226 return i - 2;
38531e6f
MG
227 }
228 return 0;
2f3d000a
YL
229}
230
231static int acpi_video_set_brightness(struct backlight_device *bd)
232{
24450c7a 233 int request_level = bd->props.brightness + 2;
7c364e79 234 struct acpi_video_device *vd = bl_get_data(bd);
24450c7a 235
8ab58e8e 236 cancel_delayed_work(&vd->switch_brightness_work);
24450c7a
ZR
237 return acpi_video_device_lcd_set_level(vd,
238 vd->brightness->levels[request_level]);
2f3d000a
YL
239}
240
acc2472e 241static const struct backlight_ops acpi_backlight_ops = {
599a52d1
RP
242 .get_brightness = acpi_video_get_brightness,
243 .update_status = acpi_video_set_brightness,
244};
245
702ed512 246/* thermal cooling device callbacks */
4a703a8f 247static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
6503e5df 248 long *state)
702ed512 249{
4a703a8f 250 struct acpi_device *device = cooling_dev->devdata;
702ed512
ZR
251 struct acpi_video_device *video = acpi_driver_data(device);
252
6503e5df
MG
253 *state = video->brightness->count - 3;
254 return 0;
702ed512
ZR
255}
256
4a703a8f 257static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
6503e5df 258 long *state)
702ed512 259{
4a703a8f 260 struct acpi_device *device = cooling_dev->devdata;
702ed512 261 struct acpi_video_device *video = acpi_driver_data(device);
27663c58 262 unsigned long long level;
6503e5df 263 int offset;
702ed512 264
a89803df 265 if (acpi_video_device_lcd_get_level_current(video, &level, false))
c8890f90 266 return -EINVAL;
6503e5df
MG
267 for (offset = 2; offset < video->brightness->count; offset++)
268 if (level == video->brightness->levels[offset]) {
269 *state = video->brightness->count - offset - 1;
270 return 0;
271 }
702ed512
ZR
272
273 return -EINVAL;
274}
275
276static int
4a703a8f 277video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
702ed512 278{
4a703a8f 279 struct acpi_device *device = cooling_dev->devdata;
702ed512
ZR
280 struct acpi_video_device *video = acpi_driver_data(device);
281 int level;
282
915ea7e4 283 if (state >= video->brightness->count - 2)
702ed512
ZR
284 return -EINVAL;
285
286 state = video->brightness->count - state;
915ea7e4 287 level = video->brightness->levels[state - 1];
702ed512
ZR
288 return acpi_video_device_lcd_set_level(video, level);
289}
290
9c8b04be 291static const struct thermal_cooling_device_ops video_cooling_ops = {
702ed512
ZR
292 .get_max_state = video_get_max_state,
293 .get_cur_state = video_get_cur_state,
294 .set_cur_state = video_set_cur_state,
295};
296
21fcb34e
FC
297/*
298 * --------------------------------------------------------------------------
299 * Video Management
300 * --------------------------------------------------------------------------
301 */
1da177e4 302
1da177e4 303static int
05950094 304acpi_video_device_lcd_query_levels(acpi_handle handle,
4be44fcd 305 union acpi_object **levels)
1da177e4 306{
4be44fcd
LB
307 int status;
308 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
309 union acpi_object *obj;
1da177e4 310
1da177e4
LT
311
312 *levels = NULL;
313
05950094 314 status = acpi_evaluate_object(handle, "_BCL", NULL, &buffer);
1da177e4 315 if (!ACPI_SUCCESS(status))
d550d98d 316 return status;
4be44fcd 317 obj = (union acpi_object *)buffer.pointer;
6665bda7 318 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6468463a 319 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
1da177e4
LT
320 status = -EFAULT;
321 goto err;
322 }
323
324 *levels = obj;
325
d550d98d 326 return 0;
1da177e4 327
915ea7e4 328err:
6044ec88 329 kfree(buffer.pointer);
1da177e4 330
d550d98d 331 return status;
1da177e4
LT
332}
333
334static int
4be44fcd 335acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
1da177e4 336{
24450c7a 337 int status;
9e6dada9 338 int state;
1da177e4 339
0db98202
JL
340 status = acpi_execute_simple_method(device->dev->handle,
341 "_BCM", level);
24450c7a
ZR
342 if (ACPI_FAILURE(status)) {
343 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
344 return -EIO;
345 }
346
4500ca8e 347 device->brightness->curr = level;
9e6dada9 348 for (state = 2; state < device->brightness->count; state++)
24450c7a 349 if (level == device->brightness->levels[state]) {
1a7c618a
ZR
350 if (device->backlight)
351 device->backlight->props.brightness = state - 2;
24450c7a
ZR
352 return 0;
353 }
9e6dada9 354
24450c7a
ZR
355 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
356 return -EINVAL;
1da177e4
LT
357}
358
45cb50e6
ZR
359/*
360 * For some buggy _BQC methods, we need to add a constant value to
361 * the _BQC return value to get the actual current brightness level
362 */
363
364static int bqc_offset_aml_bug_workaround;
7ee33baa 365static int video_set_bqc_offset(const struct dmi_system_id *d)
45cb50e6
ZR
366{
367 bqc_offset_aml_bug_workaround = 9;
368 return 0;
369}
370
7ee33baa 371static int video_disable_backlight_sysfs_if(
654a182d
HG
372 const struct dmi_system_id *d)
373{
374 if (disable_backlight_sysfs_if == -1)
375 disable_backlight_sysfs_if = 1;
376 return 0;
377}
378
e50b9be1
AL
379static int video_set_device_id_scheme(const struct dmi_system_id *d)
380{
381 device_id_scheme = true;
382 return 0;
383}
384
385static int video_enable_only_lcd(const struct dmi_system_id *d)
386{
387 only_lcd = true;
388 return 0;
389}
390
4b4b3b20
HG
391static int video_set_report_key_events(const struct dmi_system_id *id)
392{
393 if (report_key_events == -1)
394 report_key_events = (uintptr_t)id->driver_data;
395 return 0;
396}
397
7ee33baa 398static struct dmi_system_id video_dmi_table[] = {
45cb50e6
ZR
399 /*
400 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
401 */
402 {
403 .callback = video_set_bqc_offset,
404 .ident = "Acer Aspire 5720",
405 .matches = {
406 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
407 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
408 },
409 },
5afc4abe
LB
410 {
411 .callback = video_set_bqc_offset,
412 .ident = "Acer Aspire 5710Z",
413 .matches = {
414 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
415 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
416 },
417 },
34ac272b
ZR
418 {
419 .callback = video_set_bqc_offset,
420 .ident = "eMachines E510",
421 .matches = {
422 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
423 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
424 },
425 },
93bcece2
ZR
426 {
427 .callback = video_set_bqc_offset,
428 .ident = "Acer Aspire 5315",
429 .matches = {
430 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
431 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
432 },
433 },
152a4e63
ZR
434 {
435 .callback = video_set_bqc_offset,
436 .ident = "Acer Aspire 7720",
437 .matches = {
438 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
439 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
440 },
441 },
5f24079b 442
654a182d
HG
443 /*
444 * Some machines have a broken acpi-video interface for brightness
445 * control, but still need an acpi_video_device_lcd_set_level() call
446 * on resume to turn the backlight power on. We Enable backlight
447 * control on these systems, but do not register a backlight sysfs
448 * as brightness control does not work.
449 */
de588b8f
HG
450 {
451 /* https://bugzilla.kernel.org/show_bug.cgi?id=21012 */
452 .callback = video_disable_backlight_sysfs_if,
453 .ident = "Toshiba Portege R700",
454 .matches = {
455 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
456 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE R700"),
457 },
458 },
654a182d
HG
459 {
460 /* https://bugs.freedesktop.org/show_bug.cgi?id=82634 */
461 .callback = video_disable_backlight_sysfs_if,
462 .ident = "Toshiba Portege R830",
463 .matches = {
464 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
465 DMI_MATCH(DMI_PRODUCT_NAME, "PORTEGE R830"),
466 },
467 },
b21f2e81
HG
468 {
469 /* https://bugzilla.kernel.org/show_bug.cgi?id=21012 */
470 .callback = video_disable_backlight_sysfs_if,
471 .ident = "Toshiba Satellite R830",
472 .matches = {
473 DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
474 DMI_MATCH(DMI_PRODUCT_NAME, "SATELLITE R830"),
475 },
476 },
e50b9be1
AL
477 /*
478 * Some machine's _DOD IDs don't have bit 31(Device ID Scheme) set
479 * but the IDs actually follow the Device ID Scheme.
480 */
481 {
482 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
483 .callback = video_set_device_id_scheme,
484 .ident = "ESPRIMO Mobile M9410",
485 .matches = {
486 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
487 DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"),
488 },
489 },
490 /*
491 * Some machines have multiple video output devices, but only the one
492 * that is the type of LCD can do the backlight control so we should not
493 * register backlight interface for other video output devices.
494 */
495 {
496 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
497 .callback = video_enable_only_lcd,
498 .ident = "ESPRIMO Mobile M9410",
499 .matches = {
500 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
501 DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"),
502 },
503 },
4b4b3b20
HG
504 /*
505 * Some machines report wrong key events on the acpi-bus, suppress
506 * key event reporting on these. Note this is only intended to work
507 * around events which are plain wrong. In some cases we get double
508 * events, in this case acpi-video is considered the canonical source
509 * and the events from the other source should be filtered. E.g.
510 * by calling acpi_video_handles_brightness_key_presses() from the
511 * vendor acpi/wmi driver or by using /lib/udev/hwdb.d/60-keyboard.hwdb
512 */
513 {
514 .callback = video_set_report_key_events,
515 .driver_data = (void *)((uintptr_t)REPORT_OUTPUT_KEY_EVENTS),
516 .ident = "Dell Vostro V131",
517 .matches = {
518 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
519 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
520 },
521 },
45cb50e6
ZR
522 {}
523};
524
994fa63c
DB
525static unsigned long long
526acpi_video_bqc_value_to_level(struct acpi_video_device *device,
527 unsigned long long bqc_value)
528{
529 unsigned long long level;
530
531 if (device->brightness->flags._BQC_use_index) {
532 /*
533 * _BQC returns an index that doesn't account for
534 * the first 2 items with special meaning, so we need
535 * to compensate for that by offsetting ourselves
536 */
537 if (device->brightness->flags._BCL_reversed)
538 bqc_value = device->brightness->count - 3 - bqc_value;
539
540 level = device->brightness->levels[bqc_value + 2];
541 } else {
542 level = bqc_value;
543 }
544
545 level += bqc_offset_aml_bug_workaround;
546
547 return level;
548}
549
1da177e4 550static int
4be44fcd 551acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
a89803df 552 unsigned long long *level, bool raw)
1da177e4 553{
c8890f90 554 acpi_status status = AE_OK;
4e231fa4 555 int i;
c8890f90 556
c60d638e
ZR
557 if (device->cap._BQC || device->cap._BCQ) {
558 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
559
560 status = acpi_evaluate_integer(device->dev->handle, buf,
c8890f90
ZR
561 NULL, level);
562 if (ACPI_SUCCESS(status)) {
a89803df
DB
563 if (raw) {
564 /*
565 * Caller has indicated he wants the raw
566 * value returned by _BQC, so don't furtherly
567 * mess with the value.
568 */
569 return 0;
570 }
571
994fa63c 572 *level = acpi_video_bqc_value_to_level(device, *level);
1a7c618a 573
4e231fa4
VS
574 for (i = 2; i < device->brightness->count; i++)
575 if (device->brightness->levels[i] == *level) {
576 device->brightness->curr = *level;
577 return 0;
915ea7e4 578 }
a89803df
DB
579 /*
580 * BQC returned an invalid level.
581 * Stop using it.
582 */
583 ACPI_WARNING((AE_INFO,
584 "%s returned an invalid level",
585 buf));
586 device->cap._BQC = device->cap._BCQ = 0;
c8890f90 587 } else {
21fcb34e
FC
588 /*
589 * Fixme:
c8890f90
ZR
590 * should we return an error or ignore this failure?
591 * dev->brightness->curr is a cached value which stores
592 * the correct current backlight level in most cases.
593 * ACPI video backlight still works w/ buggy _BQC.
594 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
595 */
c60d638e
ZR
596 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
597 device->cap._BQC = device->cap._BCQ = 0;
c8890f90
ZR
598 }
599 }
600
4500ca8e 601 *level = device->brightness->curr;
c8890f90 602 return 0;
1da177e4
LT
603}
604
605static int
4be44fcd
LB
606acpi_video_device_EDID(struct acpi_video_device *device,
607 union acpi_object **edid, ssize_t length)
1da177e4 608{
4be44fcd
LB
609 int status;
610 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
611 union acpi_object *obj;
612 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
613 struct acpi_object_list args = { 1, &arg0 };
1da177e4 614
1da177e4
LT
615
616 *edid = NULL;
617
618 if (!device)
d550d98d 619 return -ENODEV;
1da177e4
LT
620 if (length == 128)
621 arg0.integer.value = 1;
622 else if (length == 256)
623 arg0.integer.value = 2;
624 else
d550d98d 625 return -EINVAL;
1da177e4 626
90130268 627 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
1da177e4 628 if (ACPI_FAILURE(status))
d550d98d 629 return -ENODEV;
1da177e4 630
50dd0969 631 obj = buffer.pointer;
1da177e4
LT
632
633 if (obj && obj->type == ACPI_TYPE_BUFFER)
634 *edid = obj;
635 else {
6468463a 636 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
1da177e4
LT
637 status = -EFAULT;
638 kfree(obj);
639 }
640
d550d98d 641 return status;
1da177e4
LT
642}
643
1da177e4
LT
644/* bus */
645
1da177e4
LT
646/*
647 * Arg:
21fcb34e
FC
648 * video : video bus device pointer
649 * bios_flag :
1da177e4
LT
650 * 0. The system BIOS should NOT automatically switch(toggle)
651 * the active display output.
652 * 1. The system BIOS should automatically switch (toggle) the
98fb8fe1 653 * active display output. No switch event.
1da177e4
LT
654 * 2. The _DGS value should be locked.
655 * 3. The system BIOS should not automatically switch (toggle) the
656 * active display output, but instead generate the display switch
657 * event notify code.
658 * lcd_flag :
659 * 0. The system BIOS should automatically control the brightness level
98fb8fe1 660 * of the LCD when the power changes from AC to DC
21fcb34e 661 * 1. The system BIOS should NOT automatically control the brightness
98fb8fe1 662 * level of the LCD when the power changes from AC to DC.
21fcb34e 663 * Return Value:
ea9f8856 664 * -EINVAL wrong arg.
1da177e4
LT
665 */
666
667static int
4be44fcd 668acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
1da177e4 669{
ea9f8856 670 acpi_status status;
1da177e4 671
b0373843
ZR
672 if (!video->cap._DOS)
673 return 0;
1da177e4 674
ea9f8856
IM
675 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
676 return -EINVAL;
0db98202
JL
677 video->dos_setting = (lcd_flag << 2) | bios_flag;
678 status = acpi_execute_simple_method(video->device->handle, "_DOS",
679 (lcd_flag << 2) | bios_flag);
ea9f8856
IM
680 if (ACPI_FAILURE(status))
681 return -EIO;
1da177e4 682
ea9f8856 683 return 0;
1da177e4
LT
684}
685
935e5f29
ZR
686/*
687 * Simple comparison function used to sort backlight levels.
688 */
689
690static int
691acpi_video_cmp_level(const void *a, const void *b)
692{
693 return *(int *)a - *(int *)b;
694}
695
a50188da
AL
696/*
697 * Decides if _BQC/_BCQ for this system is usable
698 *
699 * We do this by changing the level first and then read out the current
700 * brightness level, if the value does not match, find out if it is using
701 * index. If not, clear the _BQC/_BCQ capability.
702 */
703static int acpi_video_bqc_quirk(struct acpi_video_device *device,
704 int max_level, int current_level)
705{
706 struct acpi_video_device_brightness *br = device->brightness;
707 int result;
708 unsigned long long level;
709 int test_level;
710
711 /* don't mess with existing known broken systems */
712 if (bqc_offset_aml_bug_workaround)
713 return 0;
714
715 /*
716 * Some systems always report current brightness level as maximum
717 * through _BQC, we need to test another value for them.
718 */
b3b301c5 719 test_level = current_level == max_level ? br->levels[3] : max_level;
a50188da
AL
720
721 result = acpi_video_device_lcd_set_level(device, test_level);
722 if (result)
723 return result;
724
725 result = acpi_video_device_lcd_get_level_current(device, &level, true);
726 if (result)
727 return result;
728
729 if (level != test_level) {
730 /* buggy _BQC found, need to find out if it uses index */
731 if (level < br->count) {
732 if (br->flags._BCL_reversed)
733 level = br->count - 3 - level;
734 if (br->levels[level + 2] == test_level)
735 br->flags._BQC_use_index = 1;
736 }
737
738 if (!br->flags._BQC_use_index)
739 device->cap._BQC = device->cap._BCQ = 0;
740 }
741
742 return 0;
743}
744
05950094 745int acpi_video_get_levels(struct acpi_device *device,
9f9cd7ee
AL
746 struct acpi_video_device_brightness **dev_br,
747 int *pmax_level)
469778c1
JJ
748{
749 union acpi_object *obj = NULL;
d32f6947 750 int i, max_level = 0, count = 0, level_ac_battery = 0;
469778c1
JJ
751 union acpi_object *o;
752 struct acpi_video_device_brightness *br = NULL;
05950094 753 int result = 0;
bd8ba205 754 u32 value;
469778c1 755
05950094
AL
756 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device->handle,
757 &obj))) {
469778c1
JJ
758 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
759 "LCD brightness level\n"));
05950094 760 result = -ENODEV;
469778c1
JJ
761 goto out;
762 }
763
05950094
AL
764 if (obj->package.count < 2) {
765 result = -EINVAL;
469778c1 766 goto out;
05950094 767 }
469778c1
JJ
768
769 br = kzalloc(sizeof(*br), GFP_KERNEL);
770 if (!br) {
771 printk(KERN_ERR "can't allocate memory\n");
1a7c618a 772 result = -ENOMEM;
469778c1
JJ
773 goto out;
774 }
775
d32f6947 776 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
469778c1 777 GFP_KERNEL);
1a7c618a
ZR
778 if (!br->levels) {
779 result = -ENOMEM;
469778c1 780 goto out_free;
1a7c618a 781 }
469778c1
JJ
782
783 for (i = 0; i < obj->package.count; i++) {
784 o = (union acpi_object *)&obj->package.elements[i];
785 if (o->type != ACPI_TYPE_INTEGER) {
786 printk(KERN_ERR PREFIX "Invalid data\n");
787 continue;
788 }
bd8ba205
HG
789 value = (u32) o->integer.value;
790 /* Skip duplicate entries */
791 if (count > 2 && br->levels[count - 1] == value)
792 continue;
793
794 br->levels[count] = value;
469778c1
JJ
795
796 if (br->levels[count] > max_level)
797 max_level = br->levels[count];
798 count++;
799 }
800
d32f6947
ZR
801 /*
802 * some buggy BIOS don't export the levels
803 * when machine is on AC/Battery in _BCL package.
804 * In this case, the first two elements in _BCL packages
805 * are also supported brightness levels that OS should take care of.
806 */
90af2cf6
ZR
807 for (i = 2; i < count; i++) {
808 if (br->levels[i] == br->levels[0])
d32f6947 809 level_ac_battery++;
90af2cf6
ZR
810 if (br->levels[i] == br->levels[1])
811 level_ac_battery++;
812 }
d32f6947
ZR
813
814 if (level_ac_battery < 2) {
815 level_ac_battery = 2 - level_ac_battery;
816 br->flags._BCL_no_ac_battery_levels = 1;
817 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
818 br->levels[i] = br->levels[i - level_ac_battery];
819 count += level_ac_battery;
820 } else if (level_ac_battery > 2)
bf6787eb 821 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
d32f6947 822
d80fb99f
ZR
823 /* Check if the _BCL package is in a reversed order */
824 if (max_level == br->levels[2]) {
825 br->flags._BCL_reversed = 1;
826 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
827 acpi_video_cmp_level, NULL);
828 } else if (max_level != br->levels[count - 1])
829 ACPI_ERROR((AE_INFO,
bf6787eb 830 "Found unordered _BCL package"));
469778c1
JJ
831
832 br->count = count;
05950094 833 *dev_br = br;
9f9cd7ee
AL
834 if (pmax_level)
835 *pmax_level = max_level;
05950094
AL
836
837out:
838 kfree(obj);
839 return result;
840out_free:
841 kfree(br);
842 goto out;
843}
844EXPORT_SYMBOL(acpi_video_get_levels);
845
846/*
847 * Arg:
848 * device : video output device (LCD, CRT, ..)
849 *
850 * Return Value:
851 * Maximum brightness level
852 *
853 * Allocate and initialize device->brightness.
854 */
855
856static int
857acpi_video_init_brightness(struct acpi_video_device *device)
858{
859 int i, max_level = 0;
860 unsigned long long level, level_old;
861 struct acpi_video_device_brightness *br = NULL;
862 int result = -EINVAL;
863
9f9cd7ee 864 result = acpi_video_get_levels(device->dev, &br, &max_level);
05950094
AL
865 if (result)
866 return result;
469778c1 867 device->brightness = br;
1a7c618a 868
1a7c618a 869 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
90c53ca4 870 br->curr = level = max_level;
e047cca6
ZR
871
872 if (!device->cap._BQC)
873 goto set_level;
874
a89803df
DB
875 result = acpi_video_device_lcd_get_level_current(device,
876 &level_old, true);
1a7c618a
ZR
877 if (result)
878 goto out_free_levels;
879
a50188da 880 result = acpi_video_bqc_quirk(device, max_level, level_old);
1a7c618a
ZR
881 if (result)
882 goto out_free_levels;
a50188da
AL
883 /*
884 * cap._BQC may get cleared due to _BQC is found to be broken
885 * in acpi_video_bqc_quirk, so check again here.
886 */
887 if (!device->cap._BQC)
888 goto set_level;
e047cca6 889
545ef368
AL
890 level = acpi_video_bqc_value_to_level(device, level_old);
891 /*
892 * On some buggy laptops, _BQC returns an uninitialized
893 * value when invoked for the first time, i.e.
894 * level_old is invalid (no matter whether it's a level
895 * or an index). Set the backlight to max_level in this case.
896 */
897 for (i = 2; i < br->count; i++)
898 if (level == br->levels[i])
899 break;
900 if (i == br->count || !level)
901 level = max_level;
e047cca6 902
e047cca6 903set_level:
90c53ca4 904 result = acpi_video_device_lcd_set_level(device, level);
e047cca6
ZR
905 if (result)
906 goto out_free_levels;
1a7c618a 907
d32f6947 908 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
05950094
AL
909 "found %d brightness levels\n", br->count - 2));
910 return 0;
469778c1
JJ
911
912out_free_levels:
913 kfree(br->levels);
469778c1 914 kfree(br);
469778c1 915 device->brightness = NULL;
1a7c618a 916 return result;
469778c1
JJ
917}
918
919/*
920 * Arg:
921 * device : video output device (LCD, CRT, ..)
922 *
923 * Return Value:
21fcb34e 924 * None
1da177e4 925 *
98fb8fe1 926 * Find out all required AML methods defined under the output
1da177e4
LT
927 * device.
928 */
929
4be44fcd 930static void acpi_video_device_find_cap(struct acpi_video_device *device)
1da177e4 931{
952c63e9 932 if (acpi_has_method(device->dev->handle, "_ADR"))
1da177e4 933 device->cap._ADR = 1;
952c63e9 934 if (acpi_has_method(device->dev->handle, "_BCL"))
4be44fcd 935 device->cap._BCL = 1;
952c63e9 936 if (acpi_has_method(device->dev->handle, "_BCM"))
4be44fcd 937 device->cap._BCM = 1;
952c63e9 938 if (acpi_has_method(device->dev->handle, "_BQC")) {
2f3d000a 939 device->cap._BQC = 1;
952c63e9 940 } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
c60d638e
ZR
941 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
942 device->cap._BCQ = 1;
943 }
944
952c63e9 945 if (acpi_has_method(device->dev->handle, "_DDC"))
4be44fcd 946 device->cap._DDC = 1;
1da177e4
LT
947}
948
949/*
21fcb34e
FC
950 * Arg:
951 * device : video output device (VGA)
1da177e4
LT
952 *
953 * Return Value:
21fcb34e 954 * None
1da177e4 955 *
98fb8fe1 956 * Find out all required AML methods defined under the video bus device.
1da177e4
LT
957 */
958
4be44fcd 959static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
1da177e4 960{
952c63e9 961 if (acpi_has_method(video->device->handle, "_DOS"))
1da177e4 962 video->cap._DOS = 1;
952c63e9 963 if (acpi_has_method(video->device->handle, "_DOD"))
1da177e4 964 video->cap._DOD = 1;
952c63e9 965 if (acpi_has_method(video->device->handle, "_ROM"))
1da177e4 966 video->cap._ROM = 1;
952c63e9 967 if (acpi_has_method(video->device->handle, "_GPD"))
1da177e4 968 video->cap._GPD = 1;
952c63e9 969 if (acpi_has_method(video->device->handle, "_SPD"))
1da177e4 970 video->cap._SPD = 1;
952c63e9 971 if (acpi_has_method(video->device->handle, "_VPO"))
1da177e4 972 video->cap._VPO = 1;
1da177e4
LT
973}
974
975/*
976 * Check whether the video bus device has required AML method to
977 * support the desired features
978 */
979
4be44fcd 980static int acpi_video_bus_check(struct acpi_video_bus *video)
1da177e4 981{
4be44fcd 982 acpi_status status = -ENOENT;
1e4cffe7 983 struct pci_dev *dev;
1da177e4
LT
984
985 if (!video)
d550d98d 986 return -EINVAL;
1da177e4 987
1e4cffe7 988 dev = acpi_get_pci_dev(video->device->handle);
22c13f9d
TR
989 if (!dev)
990 return -ENODEV;
1e4cffe7 991 pci_dev_put(dev);
22c13f9d 992
21fcb34e
FC
993 /*
994 * Since there is no HID, CID and so on for VGA driver, we have
1da177e4
LT
995 * to check well known required nodes.
996 */
997
98fb8fe1 998 /* Does this device support video switching? */
3a1151e3
SB
999 if (video->cap._DOS || video->cap._DOD) {
1000 if (!video->cap._DOS) {
1001 printk(KERN_WARNING FW_BUG
1002 "ACPI(%s) defines _DOD but not _DOS\n",
1003 acpi_device_bid(video->device));
1004 }
1da177e4
LT
1005 video->flags.multihead = 1;
1006 status = 0;
1007 }
1008
98fb8fe1 1009 /* Does this device support retrieving a video ROM? */
4be44fcd 1010 if (video->cap._ROM) {
1da177e4
LT
1011 video->flags.rom = 1;
1012 status = 0;
1013 }
1014
98fb8fe1 1015 /* Does this device support configuring which video device to POST? */
4be44fcd 1016 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
1da177e4
LT
1017 video->flags.post = 1;
1018 status = 0;
1019 }
1020
d550d98d 1021 return status;
1da177e4
LT
1022}
1023
21fcb34e
FC
1024/*
1025 * --------------------------------------------------------------------------
1026 * Driver Interface
1027 * --------------------------------------------------------------------------
1028 */
1da177e4
LT
1029
1030/* device interface */
915ea7e4 1031static struct acpi_video_device_attrib *
82cae999
RZ
1032acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1033{
78eed028
DT
1034 struct acpi_video_enumerated_device *ids;
1035 int i;
1036
1037 for (i = 0; i < video->attached_count; i++) {
1038 ids = &video->attached_array[i];
1039 if ((ids->value.int_val & 0xffff) == device_id)
1040 return &ids->value.attrib;
1041 }
82cae999 1042
82cae999
RZ
1043 return NULL;
1044}
1da177e4 1045
e92a7162
MG
1046static int
1047acpi_video_get_device_type(struct acpi_video_bus *video,
1048 unsigned long device_id)
1049{
1050 struct acpi_video_enumerated_device *ids;
1051 int i;
1052
1053 for (i = 0; i < video->attached_count; i++) {
1054 ids = &video->attached_array[i];
1055 if ((ids->value.int_val & 0xffff) == device_id)
1056 return ids->value.int_val;
1057 }
1058
1059 return 0;
1060}
1061
1da177e4 1062static int
4be44fcd
LB
1063acpi_video_bus_get_one_device(struct acpi_device *device,
1064 struct acpi_video_bus *video)
1da177e4 1065{
27663c58 1066 unsigned long long device_id;
e92a7162 1067 int status, device_type;
4be44fcd 1068 struct acpi_video_device *data;
915ea7e4 1069 struct acpi_video_device_attrib *attribute;
1da177e4 1070
4be44fcd
LB
1071 status =
1072 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
91e13aa3
AL
1073 /* Some device omits _ADR, we skip them instead of fail */
1074 if (ACPI_FAILURE(status))
1075 return 0;
1da177e4 1076
91e13aa3
AL
1077 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1078 if (!data)
1079 return -ENOMEM;
82cae999 1080
91e13aa3
AL
1081 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1082 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1083 device->driver_data = data;
1084
1085 data->device_id = device_id;
1086 data->video = video;
1087 data->dev = device;
8ab58e8e
LT
1088 INIT_DELAYED_WORK(&data->switch_brightness_work,
1089 acpi_video_switch_brightness);
91e13aa3
AL
1090
1091 attribute = acpi_video_get_device_attr(video, device_id);
1092
e50b9be1 1093 if (attribute && (attribute->device_id_scheme || device_id_scheme)) {
91e13aa3
AL
1094 switch (attribute->display_type) {
1095 case ACPI_VIDEO_DISPLAY_CRT:
1096 data->flags.crt = 1;
1097 break;
1098 case ACPI_VIDEO_DISPLAY_TV:
1099 data->flags.tvout = 1;
1100 break;
1101 case ACPI_VIDEO_DISPLAY_DVI:
1102 data->flags.dvi = 1;
1103 break;
1104 case ACPI_VIDEO_DISPLAY_LCD:
1105 data->flags.lcd = 1;
1106 break;
1107 default:
1108 data->flags.unknown = 1;
1109 break;
1110 }
915ea7e4 1111 if (attribute->bios_can_detect)
91e13aa3
AL
1112 data->flags.bios = 1;
1113 } else {
1114 /* Check for legacy IDs */
1115 device_type = acpi_video_get_device_type(video, device_id);
1116 /* Ignore bits 16 and 18-20 */
1117 switch (device_type & 0xffe2ffff) {
915ea7e4
FC
1118 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1119 data->flags.crt = 1;
1120 break;
1121 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1122 data->flags.lcd = 1;
1123 break;
1124 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1125 data->flags.tvout = 1;
1126 break;
1127 default:
1128 data->flags.unknown = 1;
e92a7162 1129 }
91e13aa3 1130 }
4be44fcd 1131
91e13aa3
AL
1132 acpi_video_device_bind(video, data);
1133 acpi_video_device_find_cap(data);
1da177e4 1134
91e13aa3
AL
1135 mutex_lock(&video->device_list_lock);
1136 list_add_tail(&data->entry, &video->video_device_list);
1137 mutex_unlock(&video->device_list_lock);
1da177e4 1138
91e13aa3 1139 return status;
1da177e4
LT
1140}
1141
1142/*
1143 * Arg:
21fcb34e 1144 * video : video bus device
1da177e4
LT
1145 *
1146 * Return:
21fcb34e
FC
1147 * none
1148 *
1149 * Enumerate the video device list of the video bus,
1da177e4
LT
1150 * bind the ids with the corresponding video devices
1151 * under the video bus.
4be44fcd 1152 */
1da177e4 1153
4be44fcd 1154static void acpi_video_device_rebind(struct acpi_video_bus *video)
1da177e4 1155{
ff102ea9
DT
1156 struct acpi_video_device *dev;
1157
bbac81f5 1158 mutex_lock(&video->device_list_lock);
ff102ea9
DT
1159
1160 list_for_each_entry(dev, &video->video_device_list, entry)
4be44fcd 1161 acpi_video_device_bind(video, dev);
ff102ea9 1162
bbac81f5 1163 mutex_unlock(&video->device_list_lock);
1da177e4
LT
1164}
1165
1166/*
1167 * Arg:
21fcb34e
FC
1168 * video : video bus device
1169 * device : video output device under the video
1170 * bus
1da177e4
LT
1171 *
1172 * Return:
21fcb34e
FC
1173 * none
1174 *
1da177e4
LT
1175 * Bind the ids with the corresponding video devices
1176 * under the video bus.
4be44fcd 1177 */
1da177e4
LT
1178
1179static void
4be44fcd
LB
1180acpi_video_device_bind(struct acpi_video_bus *video,
1181 struct acpi_video_device *device)
1da177e4 1182{
78eed028 1183 struct acpi_video_enumerated_device *ids;
4be44fcd 1184 int i;
1da177e4 1185
78eed028
DT
1186 for (i = 0; i < video->attached_count; i++) {
1187 ids = &video->attached_array[i];
1188 if (device->device_id == (ids->value.int_val & 0xffff)) {
1189 ids->bind_info = device;
1da177e4
LT
1190 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1191 }
1192 }
1da177e4
LT
1193}
1194
0b8db271
AL
1195static bool acpi_video_device_in_dod(struct acpi_video_device *device)
1196{
1197 struct acpi_video_bus *video = device->video;
1198 int i;
1199
b4df4636
AL
1200 /*
1201 * If we have a broken _DOD or we have more than 8 output devices
1202 * under the graphics controller node that we can't proper deal with
1203 * in the operation region code currently, no need to test.
1204 */
1205 if (!video->attached_count || video->child_count > 8)
0b8db271
AL
1206 return true;
1207
1208 for (i = 0; i < video->attached_count; i++) {
35d0565b
AL
1209 if ((video->attached_array[i].value.int_val & 0xfff) ==
1210 (device->device_id & 0xfff))
0b8db271
AL
1211 return true;
1212 }
1213
1214 return false;
1215}
1216
1da177e4
LT
1217/*
1218 * Arg:
21fcb34e 1219 * video : video bus device
1da177e4
LT
1220 *
1221 * Return:
21fcb34e
FC
1222 * < 0 : error
1223 *
1da177e4
LT
1224 * Call _DOD to enumerate all devices attached to display adapter
1225 *
4be44fcd 1226 */
1da177e4
LT
1227
1228static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1229{
4be44fcd
LB
1230 int status;
1231 int count;
1232 int i;
78eed028 1233 struct acpi_video_enumerated_device *active_list;
4be44fcd
LB
1234 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1235 union acpi_object *dod = NULL;
1236 union acpi_object *obj;
1da177e4 1237
e34fbbac
AH
1238 if (!video->cap._DOD)
1239 return AE_NOT_EXIST;
1240
90130268 1241 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1da177e4 1242 if (!ACPI_SUCCESS(status)) {
a6fc6720 1243 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
d550d98d 1244 return status;
1da177e4
LT
1245 }
1246
50dd0969 1247 dod = buffer.pointer;
1da177e4 1248 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
a6fc6720 1249 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1da177e4
LT
1250 status = -EFAULT;
1251 goto out;
1252 }
1253
1254 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
4be44fcd 1255 dod->package.count));
1da177e4 1256
78eed028
DT
1257 active_list = kcalloc(1 + dod->package.count,
1258 sizeof(struct acpi_video_enumerated_device),
1259 GFP_KERNEL);
1260 if (!active_list) {
1da177e4
LT
1261 status = -ENOMEM;
1262 goto out;
1263 }
1264
1265 count = 0;
1266 for (i = 0; i < dod->package.count; i++) {
50dd0969 1267 obj = &dod->package.elements[i];
1da177e4
LT
1268
1269 if (obj->type != ACPI_TYPE_INTEGER) {
78eed028
DT
1270 printk(KERN_ERR PREFIX
1271 "Invalid _DOD data in element %d\n", i);
1272 continue;
1da177e4 1273 }
78eed028
DT
1274
1275 active_list[count].value.int_val = obj->integer.value;
1276 active_list[count].bind_info = NULL;
4be44fcd
LB
1277 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1278 (int)obj->integer.value));
1da177e4
LT
1279 count++;
1280 }
1da177e4 1281
6044ec88 1282 kfree(video->attached_array);
4be44fcd 1283
78eed028 1284 video->attached_array = active_list;
1da177e4 1285 video->attached_count = count;
78eed028 1286
915ea7e4 1287out:
02438d87 1288 kfree(buffer.pointer);
d550d98d 1289 return status;
1da177e4
LT
1290}
1291
4be44fcd
LB
1292static int
1293acpi_video_get_next_level(struct acpi_video_device *device,
1294 u32 level_current, u32 event)
1da177e4 1295{
63f0edfc 1296 int min, max, min_above, max_below, i, l, delta = 255;
f4715189
TT
1297 max = max_below = 0;
1298 min = min_above = 255;
63f0edfc 1299 /* Find closest level to level_current */
0a3db1ce 1300 for (i = 2; i < device->brightness->count; i++) {
63f0edfc
AS
1301 l = device->brightness->levels[i];
1302 if (abs(l - level_current) < abs(delta)) {
1303 delta = l - level_current;
1304 if (!delta)
1305 break;
1306 }
1307 }
1308 /* Ajust level_current to closest available level */
1309 level_current += delta;
0a3db1ce 1310 for (i = 2; i < device->brightness->count; i++) {
f4715189
TT
1311 l = device->brightness->levels[i];
1312 if (l < min)
1313 min = l;
1314 if (l > max)
1315 max = l;
1316 if (l < min_above && l > level_current)
1317 min_above = l;
1318 if (l > max_below && l < level_current)
1319 max_below = l;
1320 }
1321
1322 switch (event) {
1323 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1324 return (level_current < max) ? min_above : min;
1325 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1326 return (level_current < max) ? min_above : max;
1327 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1328 return (level_current > min) ? max_below : min;
1329 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1330 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1331 return 0;
1332 default:
1333 return level_current;
1334 }
1da177e4
LT
1335}
1336
8ab58e8e
LT
1337static void
1338acpi_video_switch_brightness(struct work_struct *work)
1da177e4 1339{
8ab58e8e
LT
1340 struct acpi_video_device *device = container_of(to_delayed_work(work),
1341 struct acpi_video_device, switch_brightness_work);
27663c58 1342 unsigned long long level_current, level_next;
8ab58e8e 1343 int event = device->switch_brightness_event;
c8890f90
ZR
1344 int result = -EINVAL;
1345
fbc9fe1b 1346 /* no warning message if acpi_backlight=vendor or a quirk is used */
654a182d 1347 if (!device->backlight)
8ab58e8e 1348 return;
28c32e99 1349
469778c1 1350 if (!device->brightness)
c8890f90
ZR
1351 goto out;
1352
1353 result = acpi_video_device_lcd_get_level_current(device,
a89803df
DB
1354 &level_current,
1355 false);
c8890f90
ZR
1356 if (result)
1357 goto out;
1358
1da177e4 1359 level_next = acpi_video_get_next_level(device, level_current, event);
c8890f90 1360
24450c7a 1361 result = acpi_video_device_lcd_set_level(device, level_next);
c8890f90 1362
36342742
MG
1363 if (!result)
1364 backlight_force_update(device->backlight,
1365 BACKLIGHT_UPDATE_HOTKEY);
1366
c8890f90
ZR
1367out:
1368 if (result)
1369 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1da177e4
LT
1370}
1371
e92a7162
MG
1372int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1373 void **edid)
1374{
1375 struct acpi_video_bus *video;
1376 struct acpi_video_device *video_device;
1377 union acpi_object *buffer = NULL;
1378 acpi_status status;
1379 int i, length;
1380
1381 if (!device || !acpi_driver_data(device))
1382 return -EINVAL;
1383
1384 video = acpi_driver_data(device);
1385
1386 for (i = 0; i < video->attached_count; i++) {
1387 video_device = video->attached_array[i].bind_info;
1388 length = 256;
1389
1390 if (!video_device)
1391 continue;
1392
82069552
ZR
1393 if (!video_device->cap._DDC)
1394 continue;
1395
e92a7162
MG
1396 if (type) {
1397 switch (type) {
1398 case ACPI_VIDEO_DISPLAY_CRT:
1399 if (!video_device->flags.crt)
1400 continue;
1401 break;
1402 case ACPI_VIDEO_DISPLAY_TV:
1403 if (!video_device->flags.tvout)
1404 continue;
1405 break;
1406 case ACPI_VIDEO_DISPLAY_DVI:
1407 if (!video_device->flags.dvi)
1408 continue;
1409 break;
1410 case ACPI_VIDEO_DISPLAY_LCD:
1411 if (!video_device->flags.lcd)
1412 continue;
1413 break;
1414 }
1415 } else if (video_device->device_id != device_id) {
1416 continue;
1417 }
1418
1419 status = acpi_video_device_EDID(video_device, &buffer, length);
1420
1421 if (ACPI_FAILURE(status) || !buffer ||
1422 buffer->type != ACPI_TYPE_BUFFER) {
1423 length = 128;
1424 status = acpi_video_device_EDID(video_device, &buffer,
1425 length);
1426 if (ACPI_FAILURE(status) || !buffer ||
1427 buffer->type != ACPI_TYPE_BUFFER) {
1428 continue;
1429 }
1430 }
1431
1432 *edid = buffer->buffer.pointer;
1433 return length;
1434 }
1435
1436 return -ENODEV;
1437}
1438EXPORT_SYMBOL(acpi_video_get_edid);
1439
1da177e4 1440static int
4be44fcd
LB
1441acpi_video_bus_get_devices(struct acpi_video_bus *video,
1442 struct acpi_device *device)
1da177e4 1443{
fba4e087 1444 int status = 0;
ff102ea9 1445 struct acpi_device *dev;
1da177e4 1446
fba4e087
IM
1447 /*
1448 * There are systems where video module known to work fine regardless
1449 * of broken _DOD and ignoring returned value here doesn't cause
1450 * any issues later.
1451 */
1452 acpi_video_device_enumerate(video);
1da177e4 1453
ff102ea9 1454 list_for_each_entry(dev, &device->children, node) {
1da177e4
LT
1455
1456 status = acpi_video_bus_get_one_device(dev, video);
ea9f8856 1457 if (status) {
91e13aa3
AL
1458 dev_err(&dev->dev, "Can't attach device\n");
1459 break;
1da177e4 1460 }
b4df4636 1461 video->child_count++;
1da177e4 1462 }
d550d98d 1463 return status;
1da177e4
LT
1464}
1465
1da177e4
LT
1466/* acpi_video interface */
1467
efaa14c7
AL
1468/*
1469 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1470 * preform any automatic brightness change on receiving a notification.
1471 */
4be44fcd 1472static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1da177e4 1473{
efaa14c7 1474 return acpi_video_bus_DOS(video, 0,
fbc9fe1b 1475 acpi_osi_is_win8() ? 1 : 0);
1da177e4
LT
1476}
1477
4be44fcd 1478static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1da177e4 1479{
efaa14c7 1480 return acpi_video_bus_DOS(video, 0,
fbc9fe1b 1481 acpi_osi_is_win8() ? 0 : 1);
1da177e4
LT
1482}
1483
7015558f 1484static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
1da177e4 1485{
7015558f 1486 struct acpi_video_bus *video = acpi_driver_data(device);
e9dab196 1487 struct input_dev *input;
17c452f9 1488 int keycode = 0;
e9dab196 1489
67b662e1 1490 if (!video || !video->input)
d550d98d 1491 return;
1da177e4 1492
e9dab196 1493 input = video->input;
1da177e4
LT
1494
1495 switch (event) {
98fb8fe1 1496 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
1da177e4 1497 * most likely via hotkey. */
8a37c65d 1498 keycode = KEY_SWITCHVIDEOMODE;
1da177e4
LT
1499 break;
1500
98fb8fe1 1501 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
1da177e4
LT
1502 * connector. */
1503 acpi_video_device_enumerate(video);
1504 acpi_video_device_rebind(video);
e9dab196 1505 keycode = KEY_SWITCHVIDEOMODE;
1da177e4
LT
1506 break;
1507
4be44fcd 1508 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
e9dab196
LY
1509 keycode = KEY_SWITCHVIDEOMODE;
1510 break;
4be44fcd 1511 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
e9dab196
LY
1512 keycode = KEY_VIDEO_NEXT;
1513 break;
4be44fcd 1514 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
e9dab196 1515 keycode = KEY_VIDEO_PREV;
1da177e4
LT
1516 break;
1517
1518 default:
1519 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 1520 "Unsupported event [0x%x]\n", event));
1da177e4
LT
1521 break;
1522 }
1523
8a37c65d
LT
1524 if (acpi_notifier_call_chain(device, event, 0))
1525 /* Something vetoed the keypress. */
1526 keycode = 0;
17c452f9 1527
05bc59a0 1528 if (keycode && (report_key_events & REPORT_OUTPUT_KEY_EVENTS)) {
17c452f9
MG
1529 input_report_key(input, keycode, 1);
1530 input_sync(input);
1531 input_report_key(input, keycode, 0);
1532 input_sync(input);
1533 }
e9dab196 1534
d550d98d 1535 return;
1da177e4
LT
1536}
1537
8ab58e8e
LT
1538static void brightness_switch_event(struct acpi_video_device *video_device,
1539 u32 event)
1540{
1541 if (!brightness_switch_enabled)
1542 return;
1543
1544 video_device->switch_brightness_event = event;
1545 schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
1546}
1547
4be44fcd 1548static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1da177e4 1549{
50dd0969 1550 struct acpi_video_device *video_device = data;
4be44fcd 1551 struct acpi_device *device = NULL;
e9dab196
LY
1552 struct acpi_video_bus *bus;
1553 struct input_dev *input;
17c452f9 1554 int keycode = 0;
1da177e4 1555
1da177e4 1556 if (!video_device)
d550d98d 1557 return;
1da177e4 1558
e6afa0de 1559 device = video_device->dev;
e9dab196
LY
1560 bus = video_device->video;
1561 input = bus->input;
1da177e4
LT
1562
1563 switch (event) {
4be44fcd 1564 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
8ab58e8e 1565 brightness_switch_event(video_device, event);
e9dab196
LY
1566 keycode = KEY_BRIGHTNESS_CYCLE;
1567 break;
4be44fcd 1568 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
8ab58e8e 1569 brightness_switch_event(video_device, event);
e9dab196
LY
1570 keycode = KEY_BRIGHTNESSUP;
1571 break;
4be44fcd 1572 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
8ab58e8e 1573 brightness_switch_event(video_device, event);
e9dab196
LY
1574 keycode = KEY_BRIGHTNESSDOWN;
1575 break;
70f23fd6 1576 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
8ab58e8e 1577 brightness_switch_event(video_device, event);
e9dab196
LY
1578 keycode = KEY_BRIGHTNESS_ZERO;
1579 break;
4be44fcd 1580 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
8ab58e8e 1581 brightness_switch_event(video_device, event);
e9dab196 1582 keycode = KEY_DISPLAY_OFF;
1da177e4
LT
1583 break;
1584 default:
1585 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
4be44fcd 1586 "Unsupported event [0x%x]\n", event));
1da177e4
LT
1587 break;
1588 }
e9dab196 1589
7761f638 1590 acpi_notifier_call_chain(device, event, 0);
17c452f9 1591
05bc59a0 1592 if (keycode && (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS)) {
17c452f9
MG
1593 input_report_key(input, keycode, 1);
1594 input_sync(input);
1595 input_report_key(input, keycode, 0);
1596 input_sync(input);
1597 }
e9dab196 1598
d550d98d 1599 return;
1da177e4
LT
1600}
1601
ac7729da
RW
1602static int acpi_video_resume(struct notifier_block *nb,
1603 unsigned long val, void *ign)
863c1490
MG
1604{
1605 struct acpi_video_bus *video;
1606 struct acpi_video_device *video_device;
1607 int i;
1608
ac7729da
RW
1609 switch (val) {
1610 case PM_HIBERNATION_PREPARE:
1611 case PM_SUSPEND_PREPARE:
1612 case PM_RESTORE_PREPARE:
1613 return NOTIFY_DONE;
1614 }
863c1490 1615
ac7729da
RW
1616 video = container_of(nb, struct acpi_video_bus, pm_nb);
1617
1618 dev_info(&video->device->dev, "Restoring backlight state\n");
863c1490
MG
1619
1620 for (i = 0; i < video->attached_count; i++) {
1621 video_device = video->attached_array[i].bind_info;
654a182d
HG
1622 if (video_device && video_device->brightness)
1623 acpi_video_device_lcd_set_level(video_device,
1624 video_device->brightness->curr);
863c1490 1625 }
ac7729da
RW
1626
1627 return NOTIFY_OK;
863c1490
MG
1628}
1629
c504f8cb
ZR
1630static acpi_status
1631acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1632 void **return_value)
1633{
1634 struct acpi_device *device = context;
1635 struct acpi_device *sibling;
1636 int result;
1637
1638 if (handle == device->handle)
1639 return AE_CTRL_TERMINATE;
1640
1641 result = acpi_bus_get_device(handle, &sibling);
1642 if (result)
1643 return AE_OK;
1644
1645 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1646 return AE_ALREADY_EXISTS;
1647
1648 return AE_OK;
1649}
1650
67b662e1
AL
1651static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
1652{
99678ed7
HG
1653 struct backlight_properties props;
1654 struct pci_dev *pdev;
1655 acpi_handle acpi_parent;
1656 struct device *parent = NULL;
1657 int result;
1658 static int count;
1659 char *name;
67b662e1 1660
99678ed7
HG
1661 result = acpi_video_init_brightness(device);
1662 if (result)
1663 return;
654a182d
HG
1664
1665 if (disable_backlight_sysfs_if > 0)
1666 return;
1667
99678ed7
HG
1668 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
1669 if (!name)
1670 return;
1671 count++;
67b662e1 1672
99678ed7 1673 acpi_get_parent(device->dev->handle, &acpi_parent);
67b662e1 1674
99678ed7
HG
1675 pdev = acpi_get_pci_dev(acpi_parent);
1676 if (pdev) {
1677 parent = &pdev->dev;
1678 pci_dev_put(pdev);
1679 }
67b662e1 1680
99678ed7
HG
1681 memset(&props, 0, sizeof(struct backlight_properties));
1682 props.type = BACKLIGHT_FIRMWARE;
1683 props.max_brightness = device->brightness->count - 3;
1684 device->backlight = backlight_device_register(name,
1685 parent,
1686 device,
1687 &acpi_backlight_ops,
1688 &props);
1689 kfree(name);
654a182d
HG
1690 if (IS_ERR(device->backlight)) {
1691 device->backlight = NULL;
99678ed7 1692 return;
654a182d 1693 }
67b662e1 1694
99678ed7
HG
1695 /*
1696 * Save current brightness level in case we have to restore it
1697 * before acpi_video_device_lcd_set_level() is called next time.
1698 */
1699 device->backlight->props.brightness =
1700 acpi_video_get_brightness(device->backlight);
67b662e1 1701
99678ed7
HG
1702 device->cooling_dev = thermal_cooling_device_register("LCD",
1703 device->dev, &video_cooling_ops);
1704 if (IS_ERR(device->cooling_dev)) {
1705 /*
1706 * Set cooling_dev to NULL so we don't crash trying to free it.
1707 * Also, why the hell we are returning early and not attempt to
1708 * register video output if cooling device registration failed?
1709 * -- dtor
1710 */
1711 device->cooling_dev = NULL;
1712 return;
67b662e1 1713 }
99678ed7
HG
1714
1715 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1716 device->cooling_dev->id);
1717 result = sysfs_create_link(&device->dev->dev.kobj,
1718 &device->cooling_dev->device.kobj,
1719 "thermal_cooling");
1720 if (result)
1721 printk(KERN_ERR PREFIX "Create sysfs link\n");
1722 result = sysfs_create_link(&device->cooling_dev->device.kobj,
1723 &device->dev->dev.kobj, "device");
1724 if (result)
1725 printk(KERN_ERR PREFIX "Create sysfs link\n");
67b662e1
AL
1726}
1727
dce4ec2e
AL
1728static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
1729{
1730 struct acpi_video_device *dev;
1731 union acpi_object *levels;
1732
1733 mutex_lock(&video->device_list_lock);
1734 list_for_each_entry(dev, &video->video_device_list, entry) {
9f9cd7ee 1735 if (!acpi_video_device_lcd_query_levels(dev->dev->handle, &levels))
dce4ec2e
AL
1736 kfree(levels);
1737 }
1738 mutex_unlock(&video->device_list_lock);
1739}
1740
e50b9be1
AL
1741static bool acpi_video_should_register_backlight(struct acpi_video_device *dev)
1742{
1743 /*
1744 * Do not create backlight device for video output
1745 * device that is not in the enumerated list.
1746 */
1747 if (!acpi_video_device_in_dod(dev)) {
1748 dev_dbg(&dev->dev->dev, "not in _DOD list, ignore\n");
1749 return false;
1750 }
1751
1752 if (only_lcd)
1753 return dev->flags.lcd;
1754 return true;
1755}
1756
67b662e1
AL
1757static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
1758{
1759 struct acpi_video_device *dev;
1760
53a92ba7
HG
1761 if (video->backlight_registered)
1762 return 0;
1763
dce4ec2e
AL
1764 acpi_video_run_bcl_for_osi(video);
1765
3bd6bce3 1766 if (acpi_video_get_backlight_type() != acpi_backlight_video)
99678ed7
HG
1767 return 0;
1768
67b662e1 1769 mutex_lock(&video->device_list_lock);
e50b9be1
AL
1770 list_for_each_entry(dev, &video->video_device_list, entry) {
1771 if (acpi_video_should_register_backlight(dev))
1772 acpi_video_dev_register_backlight(dev);
1773 }
67b662e1
AL
1774 mutex_unlock(&video->device_list_lock);
1775
99678ed7
HG
1776 video->backlight_registered = true;
1777
67b662e1
AL
1778 video->pm_nb.notifier_call = acpi_video_resume;
1779 video->pm_nb.priority = 0;
1780 return register_pm_notifier(&video->pm_nb);
1781}
1782
1783static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
1784{
1785 if (device->backlight) {
1786 backlight_device_unregister(device->backlight);
1787 device->backlight = NULL;
1788 }
1789 if (device->brightness) {
1790 kfree(device->brightness->levels);
1791 kfree(device->brightness);
1792 device->brightness = NULL;
1793 }
1794 if (device->cooling_dev) {
1795 sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
1796 sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
1797 thermal_cooling_device_unregister(device->cooling_dev);
1798 device->cooling_dev = NULL;
1799 }
1800}
1801
1802static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
1803{
1804 struct acpi_video_device *dev;
99678ed7
HG
1805 int error;
1806
1807 if (!video->backlight_registered)
1808 return 0;
1809
1810 error = unregister_pm_notifier(&video->pm_nb);
67b662e1
AL
1811
1812 mutex_lock(&video->device_list_lock);
1813 list_for_each_entry(dev, &video->video_device_list, entry)
1814 acpi_video_dev_unregister_backlight(dev);
1815 mutex_unlock(&video->device_list_lock);
1816
99678ed7
HG
1817 video->backlight_registered = false;
1818
67b662e1
AL
1819 return error;
1820}
1821
1822static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
1823{
1824 acpi_status status;
1825 struct acpi_device *adev = device->dev;
1826
1827 status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
1828 acpi_video_device_notify, device);
1829 if (ACPI_FAILURE(status))
1830 dev_err(&adev->dev, "Error installing notify handler\n");
1831 else
1832 device->flags.notify = 1;
1833}
1834
1835static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
1836{
1837 struct input_dev *input;
1838 struct acpi_video_device *dev;
1839 int error;
1840
1841 video->input = input = input_allocate_device();
1842 if (!input) {
1843 error = -ENOMEM;
1844 goto out;
1845 }
1846
1847 error = acpi_video_bus_start_devices(video);
1848 if (error)
1849 goto err_free_input;
1850
1851 snprintf(video->phys, sizeof(video->phys),
1852 "%s/video/input0", acpi_device_hid(video->device));
1853
1854 input->name = acpi_device_name(video->device);
1855 input->phys = video->phys;
1856 input->id.bustype = BUS_HOST;
1857 input->id.product = 0x06;
1858 input->dev.parent = &video->device->dev;
1859 input->evbit[0] = BIT(EV_KEY);
1860 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1861 set_bit(KEY_VIDEO_NEXT, input->keybit);
1862 set_bit(KEY_VIDEO_PREV, input->keybit);
1863 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1864 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1865 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1866 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1867 set_bit(KEY_DISPLAY_OFF, input->keybit);
1868
1869 error = input_register_device(input);
1870 if (error)
1871 goto err_stop_dev;
1872
1873 mutex_lock(&video->device_list_lock);
1874 list_for_each_entry(dev, &video->video_device_list, entry)
1875 acpi_video_dev_add_notify_handler(dev);
1876 mutex_unlock(&video->device_list_lock);
1877
1878 return 0;
1879
1880err_stop_dev:
1881 acpi_video_bus_stop_devices(video);
1882err_free_input:
1883 input_free_device(input);
1884 video->input = NULL;
1885out:
1886 return error;
1887}
1888
1889static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
1890{
1891 if (dev->flags.notify) {
1892 acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
1893 acpi_video_device_notify);
1894 dev->flags.notify = 0;
1895 }
1896}
1897
1898static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
1899{
1900 struct acpi_video_device *dev;
1901
1902 mutex_lock(&video->device_list_lock);
1903 list_for_each_entry(dev, &video->video_device_list, entry)
1904 acpi_video_dev_remove_notify_handler(dev);
1905 mutex_unlock(&video->device_list_lock);
1906
1907 acpi_video_bus_stop_devices(video);
1908 input_unregister_device(video->input);
1909 video->input = NULL;
1910}
1911
1912static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1913{
1914 struct acpi_video_device *dev, *next;
1915
1916 mutex_lock(&video->device_list_lock);
1917 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1918 list_del(&dev->entry);
1919 kfree(dev);
1920 }
1921 mutex_unlock(&video->device_list_lock);
1922
1923 return 0;
1924}
1925
ac7729da
RW
1926static int instance;
1927
4be44fcd 1928static int acpi_video_bus_add(struct acpi_device *device)
1da177e4 1929{
f51e8391 1930 struct acpi_video_bus *video;
f51e8391 1931 int error;
c504f8cb
ZR
1932 acpi_status status;
1933
1934 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1935 device->parent->handle, 1,
1936 acpi_video_bus_match, NULL,
1937 device, NULL);
1938 if (status == AE_ALREADY_EXISTS) {
1939 printk(KERN_WARNING FW_BUG
1940 "Duplicate ACPI video bus devices for the"
1941 " same VGA controller, please try module "
1942 "parameter \"video.allow_duplicates=1\""
1943 "if the current driver doesn't work.\n");
1944 if (!allow_duplicates)
1945 return -ENODEV;
1946 }
1da177e4 1947
36bcbec7 1948 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1da177e4 1949 if (!video)
d550d98d 1950 return -ENOMEM;
1da177e4 1951
e6d9da1d
ZR
1952 /* a hack to fix the duplicate name "VID" problem on T61 */
1953 if (!strcmp(device->pnp.bus_id, "VID")) {
1954 if (instance)
1955 device->pnp.bus_id[3] = '0' + instance;
915ea7e4 1956 instance++;
e6d9da1d 1957 }
f3b39f13
ZY
1958 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1959 if (!strcmp(device->pnp.bus_id, "VGA")) {
1960 if (instance)
1961 device->pnp.bus_id[3] = '0' + instance;
1962 instance++;
1963 }
e6d9da1d 1964
e6afa0de 1965 video->device = device;
1da177e4
LT
1966 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1967 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
db89b4f0 1968 device->driver_data = video;
1da177e4
LT
1969
1970 acpi_video_bus_find_cap(video);
f51e8391
DT
1971 error = acpi_video_bus_check(video);
1972 if (error)
1973 goto err_free_video;
1da177e4 1974
bbac81f5 1975 mutex_init(&video->device_list_lock);
1da177e4
LT
1976 INIT_LIST_HEAD(&video->video_device_list);
1977
ea9f8856
IM
1978 error = acpi_video_bus_get_devices(video, device);
1979 if (error)
91e13aa3 1980 goto err_put_video;
1da177e4 1981
1da177e4 1982 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
4be44fcd
LB
1983 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1984 video->flags.multihead ? "yes" : "no",
1985 video->flags.rom ? "yes" : "no",
1986 video->flags.post ? "yes" : "no");
67b662e1
AL
1987 mutex_lock(&video_list_lock);
1988 list_add_tail(&video->entry, &video_bus_head);
1989 mutex_unlock(&video_list_lock);
1da177e4 1990
67b662e1
AL
1991 acpi_video_bus_register_backlight(video);
1992 acpi_video_bus_add_notify_handler(video);
ac7729da 1993
f51e8391
DT
1994 return 0;
1995
67b662e1 1996err_put_video:
f51e8391
DT
1997 acpi_video_bus_put_devices(video);
1998 kfree(video->attached_array);
67b662e1 1999err_free_video:
f51e8391 2000 kfree(video);
db89b4f0 2001 device->driver_data = NULL;
1da177e4 2002
f51e8391 2003 return error;
1da177e4
LT
2004}
2005
51fac838 2006static int acpi_video_bus_remove(struct acpi_device *device)
1da177e4 2007{
4be44fcd 2008 struct acpi_video_bus *video = NULL;
1da177e4 2009
1da177e4
LT
2010
2011 if (!device || !acpi_driver_data(device))
d550d98d 2012 return -EINVAL;
1da177e4 2013
50dd0969 2014 video = acpi_driver_data(device);
1da177e4 2015
67b662e1
AL
2016 acpi_video_bus_remove_notify_handler(video);
2017 acpi_video_bus_unregister_backlight(video);
1da177e4 2018 acpi_video_bus_put_devices(video);
1da177e4 2019
67b662e1
AL
2020 mutex_lock(&video_list_lock);
2021 list_del(&video->entry);
2022 mutex_unlock(&video_list_lock);
2023
6044ec88 2024 kfree(video->attached_array);
1da177e4
LT
2025 kfree(video);
2026
d550d98d 2027 return 0;
1da177e4
LT
2028}
2029
c6996bdd
AC
2030static int __init is_i740(struct pci_dev *dev)
2031{
2032 if (dev->device == 0x00D1)
2033 return 1;
2034 if (dev->device == 0x7000)
2035 return 1;
2036 return 0;
2037}
2038
74a365b3
MG
2039static int __init intel_opregion_present(void)
2040{
c6996bdd 2041 int opregion = 0;
74a365b3
MG
2042 struct pci_dev *dev = NULL;
2043 u32 address;
2044
2045 for_each_pci_dev(dev) {
2046 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2047 continue;
2048 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2049 continue;
c6996bdd
AC
2050 /* We don't want to poke around undefined i740 registers */
2051 if (is_i740(dev))
2052 continue;
74a365b3
MG
2053 pci_read_config_dword(dev, 0xfc, &address);
2054 if (!address)
2055 continue;
c6996bdd 2056 opregion = 1;
74a365b3 2057 }
c6996bdd 2058 return opregion;
74a365b3
MG
2059}
2060
8e5c2b77 2061int acpi_video_register(void)
1da177e4 2062{
2a8b18e9 2063 int ret = 0;
28d63403 2064
970530cd
HG
2065 mutex_lock(&register_count_mutex);
2066 if (register_count) {
86e437f0 2067 /*
8e5c2b77
RW
2068 * if the function of acpi_video_register is already called,
2069 * don't register the acpi_vide_bus again and return no error.
86e437f0 2070 */
2a8b18e9 2071 goto leave;
86e437f0 2072 }
1da177e4 2073
7ee33baa
HG
2074 dmi_check_system(video_dmi_table);
2075
28d63403
CW
2076 ret = acpi_bus_register_driver(&acpi_video_bus);
2077 if (ret)
2a8b18e9 2078 goto leave;
1da177e4 2079
86e437f0
ZY
2080 /*
2081 * When the acpi_video_bus is loaded successfully, increase
2082 * the counter reference.
2083 */
970530cd 2084 register_count = 1;
86e437f0 2085
2a8b18e9 2086leave:
970530cd 2087 mutex_unlock(&register_count_mutex);
2a8b18e9 2088 return ret;
1da177e4 2089}
8e5c2b77 2090EXPORT_SYMBOL(acpi_video_register);
74a365b3 2091
86e437f0
ZY
2092void acpi_video_unregister(void)
2093{
970530cd
HG
2094 mutex_lock(&register_count_mutex);
2095 if (register_count) {
2a8b18e9 2096 acpi_bus_unregister_driver(&acpi_video_bus);
970530cd 2097 register_count = 0;
86e437f0 2098 }
970530cd 2099 mutex_unlock(&register_count_mutex);
86e437f0
ZY
2100}
2101EXPORT_SYMBOL(acpi_video_unregister);
2102
1b7f37e1
HG
2103void acpi_video_unregister_backlight(void)
2104{
2105 struct acpi_video_bus *video;
2106
970530cd
HG
2107 mutex_lock(&register_count_mutex);
2108 if (register_count) {
2a8b18e9
HG
2109 mutex_lock(&video_list_lock);
2110 list_for_each_entry(video, &video_bus_head, entry)
2111 acpi_video_bus_unregister_backlight(video);
2112 mutex_unlock(&video_list_lock);
2113 }
970530cd 2114 mutex_unlock(&register_count_mutex);
1b7f37e1 2115}
1b7f37e1 2116
90b066b1
HG
2117bool acpi_video_handles_brightness_key_presses(void)
2118{
2119 bool have_video_busses;
2120
2121 mutex_lock(&video_list_lock);
2122 have_video_busses = !list_empty(&video_bus_head);
2123 mutex_unlock(&video_list_lock);
2124
05bc59a0
HG
2125 return have_video_busses &&
2126 (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS);
90b066b1
HG
2127}
2128EXPORT_SYMBOL(acpi_video_handles_brightness_key_presses);
2129
74a365b3
MG
2130/*
2131 * This is kind of nasty. Hardware using Intel chipsets may require
2132 * the video opregion code to be run first in order to initialise
2133 * state before any ACPI video calls are made. To handle this we defer
2134 * registration of the video class until the opregion code has run.
2135 */
2136
2137static int __init acpi_video_init(void)
2138{
6e17cb12
CW
2139 /*
2140 * Let the module load even if ACPI is disabled (e.g. due to
2141 * a broken BIOS) so that i915.ko can still be loaded on such
2142 * old systems without an AcpiOpRegion.
2143 *
2144 * acpi_video_register() will report -ENODEV later as well due
2145 * to acpi_disabled when i915.ko tries to register itself afterwards.
2146 */
2147 if (acpi_disabled)
2148 return 0;
2149
74a365b3
MG
2150 if (intel_opregion_present())
2151 return 0;
2152
2153 return acpi_video_register();
2154}
1da177e4 2155
86e437f0 2156static void __exit acpi_video_exit(void)
1da177e4 2157{
93a291df 2158 acpi_video_detect_exit();
86e437f0 2159 acpi_video_unregister();
1da177e4 2160
d550d98d 2161 return;
1da177e4
LT
2162}
2163
2164module_init(acpi_video_init);
2165module_exit(acpi_video_exit);