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