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