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