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