Linux 4.1.16
[linux/fpc-iii.git] / drivers / acpi / video.c
blobcc79d3fedfb2a33aa8ca273865c1bf84ec2b190b
1 /*
2 * video.c - ACPI Video Driver
4 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5 * Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
6 * Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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>
32 #include <linux/mutex.h>
33 #include <linux/input.h>
34 #include <linux/backlight.h>
35 #include <linux/thermal.h>
36 #include <linux/sort.h>
37 #include <linux/pci.h>
38 #include <linux/pci_ids.h>
39 #include <linux/slab.h>
40 #include <linux/dmi.h>
41 #include <linux/suspend.h>
42 #include <linux/acpi.h>
43 #include <acpi/video.h>
44 #include <asm/uaccess.h>
46 #include "internal.h"
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
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
62 #define MAX_NAME_LEN 20
64 #define _COMPONENT ACPI_VIDEO_COMPONENT
65 ACPI_MODULE_NAME("video");
67 MODULE_AUTHOR("Bruno Ducrot");
68 MODULE_DESCRIPTION("ACPI Video Driver");
69 MODULE_LICENSE("GPL");
71 static bool brightness_switch_enabled = 1;
72 module_param(brightness_switch_enabled, bool, 0644);
75 * By default, we don't allow duplicate ACPI video bus devices
76 * under the same VGA controller
78 static bool allow_duplicates;
79 module_param(allow_duplicates, bool, 0644);
82 * For Windows 8 systems: used to decide if video module
83 * should skip registering backlight interface of its own.
85 enum {
86 NATIVE_BACKLIGHT_NOT_SET = -1,
87 NATIVE_BACKLIGHT_OFF,
88 NATIVE_BACKLIGHT_ON,
91 static int use_native_backlight_param = NATIVE_BACKLIGHT_NOT_SET;
92 module_param_named(use_native_backlight, use_native_backlight_param, int, 0444);
93 static int use_native_backlight_dmi = NATIVE_BACKLIGHT_NOT_SET;
95 static int register_count;
96 static struct mutex video_list_lock;
97 static struct list_head video_bus_head;
98 static int acpi_video_bus_add(struct acpi_device *device);
99 static int acpi_video_bus_remove(struct acpi_device *device);
100 static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
102 static const struct acpi_device_id video_device_ids[] = {
103 {ACPI_VIDEO_HID, 0},
104 {"", 0},
106 MODULE_DEVICE_TABLE(acpi, video_device_ids);
108 static struct acpi_driver acpi_video_bus = {
109 .name = "video",
110 .class = ACPI_VIDEO_CLASS,
111 .ids = video_device_ids,
112 .ops = {
113 .add = acpi_video_bus_add,
114 .remove = acpi_video_bus_remove,
115 .notify = acpi_video_bus_notify,
119 struct acpi_video_bus_flags {
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;
126 struct acpi_video_bus_cap {
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 */
133 u8 reserved:2;
136 struct acpi_video_device_attrib {
137 u32 display_index:4; /* A zero-based instance of the Display */
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
143 the VGA device. */
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 */
149 struct acpi_video_enumerated_device {
150 union {
151 u32 int_val;
152 struct acpi_video_device_attrib attrib;
153 } value;
154 struct acpi_video_device *bind_info;
157 struct acpi_video_bus {
158 struct acpi_device *device;
159 bool backlight_registered;
160 bool backlight_notifier_registered;
161 u8 dos_setting;
162 struct acpi_video_enumerated_device *attached_array;
163 u8 attached_count;
164 u8 child_count;
165 struct acpi_video_bus_cap cap;
166 struct acpi_video_bus_flags flags;
167 struct list_head video_device_list;
168 struct mutex device_list_lock; /* protects video_device_list */
169 struct list_head entry;
170 struct input_dev *input;
171 char phys[32]; /* for input device */
172 struct notifier_block pm_nb;
173 struct notifier_block backlight_nb;
176 struct acpi_video_device_flags {
177 u8 crt:1;
178 u8 lcd:1;
179 u8 tvout:1;
180 u8 dvi:1;
181 u8 bios:1;
182 u8 unknown:1;
183 u8 notify:1;
184 u8 reserved:1;
187 struct acpi_video_device_cap {
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 */
191 u8 _BQC:1; /* Get current brightness level */
192 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
193 u8 _DDC:1; /* Return the EDID for this device */
196 struct acpi_video_brightness_flags {
197 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
198 u8 _BCL_reversed:1; /* _BCL package is in a reversed order */
199 u8 _BQC_use_index:1; /* _BQC returns an index value */
202 struct acpi_video_device_brightness {
203 int curr;
204 int count;
205 int *levels;
206 struct acpi_video_brightness_flags flags;
209 struct acpi_video_device {
210 unsigned long device_id;
211 struct acpi_video_device_flags flags;
212 struct acpi_video_device_cap cap;
213 struct list_head entry;
214 struct delayed_work switch_brightness_work;
215 int switch_brightness_event;
216 struct acpi_video_bus *video;
217 struct acpi_device *dev;
218 struct acpi_video_device_brightness *brightness;
219 struct backlight_device *backlight;
220 struct thermal_cooling_device *cooling_dev;
223 static const char device_decode[][30] = {
224 "motherboard VGA device",
225 "PCI VGA device",
226 "AGP VGA device",
227 "UNKNOWN",
230 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
231 static void acpi_video_device_rebind(struct acpi_video_bus *video);
232 static void acpi_video_device_bind(struct acpi_video_bus *video,
233 struct acpi_video_device *device);
234 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
235 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
236 int level);
237 static int acpi_video_device_lcd_get_level_current(
238 struct acpi_video_device *device,
239 unsigned long long *level, bool raw);
240 static int acpi_video_get_next_level(struct acpi_video_device *device,
241 u32 level_current, u32 event);
242 static void acpi_video_switch_brightness(struct work_struct *work);
244 static bool acpi_video_use_native_backlight(void)
246 if (use_native_backlight_param != NATIVE_BACKLIGHT_NOT_SET)
247 return use_native_backlight_param;
248 else if (use_native_backlight_dmi != NATIVE_BACKLIGHT_NOT_SET)
249 return use_native_backlight_dmi;
250 return acpi_osi_is_win8();
253 bool acpi_video_verify_backlight_support(void)
255 if (acpi_video_use_native_backlight() &&
256 backlight_device_registered(BACKLIGHT_RAW))
257 return false;
258 return acpi_video_backlight_support();
260 EXPORT_SYMBOL_GPL(acpi_video_verify_backlight_support);
262 /* backlight device sysfs support */
263 static int acpi_video_get_brightness(struct backlight_device *bd)
265 unsigned long long cur_level;
266 int i;
267 struct acpi_video_device *vd = bl_get_data(bd);
269 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
270 return -EINVAL;
271 for (i = 2; i < vd->brightness->count; i++) {
272 if (vd->brightness->levels[i] == cur_level)
274 * The first two entries are special - see page 575
275 * of the ACPI spec 3.0
277 return i - 2;
279 return 0;
282 static int acpi_video_set_brightness(struct backlight_device *bd)
284 int request_level = bd->props.brightness + 2;
285 struct acpi_video_device *vd = bl_get_data(bd);
287 cancel_delayed_work(&vd->switch_brightness_work);
288 return acpi_video_device_lcd_set_level(vd,
289 vd->brightness->levels[request_level]);
292 static const struct backlight_ops acpi_backlight_ops = {
293 .get_brightness = acpi_video_get_brightness,
294 .update_status = acpi_video_set_brightness,
297 /* thermal cooling device callbacks */
298 static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
299 long *state)
301 struct acpi_device *device = cooling_dev->devdata;
302 struct acpi_video_device *video = acpi_driver_data(device);
304 *state = video->brightness->count - 3;
305 return 0;
308 static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
309 long *state)
311 struct acpi_device *device = cooling_dev->devdata;
312 struct acpi_video_device *video = acpi_driver_data(device);
313 unsigned long long level;
314 int offset;
316 if (acpi_video_device_lcd_get_level_current(video, &level, false))
317 return -EINVAL;
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;
324 return -EINVAL;
327 static int
328 video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
330 struct acpi_device *device = cooling_dev->devdata;
331 struct acpi_video_device *video = acpi_driver_data(device);
332 int level;
334 if (state >= video->brightness->count - 2)
335 return -EINVAL;
337 state = video->brightness->count - state;
338 level = video->brightness->levels[state - 1];
339 return acpi_video_device_lcd_set_level(video, level);
342 static const struct thermal_cooling_device_ops video_cooling_ops = {
343 .get_max_state = video_get_max_state,
344 .get_cur_state = video_get_cur_state,
345 .set_cur_state = video_set_cur_state,
349 * --------------------------------------------------------------------------
350 * Video Management
351 * --------------------------------------------------------------------------
354 static int
355 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
356 union acpi_object **levels)
358 int status;
359 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
360 union acpi_object *obj;
363 *levels = NULL;
365 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
366 if (!ACPI_SUCCESS(status))
367 return status;
368 obj = (union acpi_object *)buffer.pointer;
369 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
370 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
371 status = -EFAULT;
372 goto err;
375 *levels = obj;
377 return 0;
379 err:
380 kfree(buffer.pointer);
382 return status;
385 static int
386 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
388 int status;
389 int state;
391 status = acpi_execute_simple_method(device->dev->handle,
392 "_BCM", level);
393 if (ACPI_FAILURE(status)) {
394 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
395 return -EIO;
398 device->brightness->curr = level;
399 for (state = 2; state < device->brightness->count; state++)
400 if (level == device->brightness->levels[state]) {
401 if (device->backlight)
402 device->backlight->props.brightness = state - 2;
403 return 0;
406 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
407 return -EINVAL;
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
415 static int bqc_offset_aml_bug_workaround;
416 static int __init video_set_bqc_offset(const struct dmi_system_id *d)
418 bqc_offset_aml_bug_workaround = 9;
419 return 0;
422 static int __init video_disable_native_backlight(const struct dmi_system_id *d)
424 use_native_backlight_dmi = NATIVE_BACKLIGHT_OFF;
425 return 0;
428 static int __init video_enable_native_backlight(const struct dmi_system_id *d)
430 use_native_backlight_dmi = NATIVE_BACKLIGHT_ON;
431 return 0;
434 static struct dmi_system_id video_dmi_table[] __initdata = {
436 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
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"),
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"),
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"),
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"),
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"),
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
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"),
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"),
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"),
511 /* The native backlight controls do not work on some older machines */
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"),
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"),
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"),
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"),
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"),
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"),
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"),
576 /* Non win8 machines which need native backlight nevertheless */
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"),
589 static unsigned long long
590 acpi_video_bqc_value_to_level(struct acpi_video_device *device,
591 unsigned long long bqc_value)
593 unsigned long long level;
595 if (device->brightness->flags._BQC_use_index) {
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
601 if (device->brightness->flags._BCL_reversed)
602 bqc_value = device->brightness->count - 3 - bqc_value;
604 level = device->brightness->levels[bqc_value + 2];
605 } else {
606 level = bqc_value;
609 level += bqc_offset_aml_bug_workaround;
611 return level;
614 static int
615 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
616 unsigned long long *level, bool raw)
618 acpi_status status = AE_OK;
619 int i;
621 if (device->cap._BQC || device->cap._BCQ) {
622 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
624 status = acpi_evaluate_integer(device->dev->handle, buf,
625 NULL, level);
626 if (ACPI_SUCCESS(status)) {
627 if (raw) {
629 * Caller has indicated he wants the raw
630 * value returned by _BQC, so don't furtherly
631 * mess with the value.
633 return 0;
636 *level = acpi_video_bqc_value_to_level(device, *level);
638 for (i = 2; i < device->brightness->count; i++)
639 if (device->brightness->levels[i] == *level) {
640 device->brightness->curr = *level;
641 return 0;
644 * BQC returned an invalid level.
645 * Stop using it.
647 ACPI_WARNING((AE_INFO,
648 "%s returned an invalid level",
649 buf));
650 device->cap._BQC = device->cap._BCQ = 0;
651 } else {
653 * Fixme:
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
660 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
661 device->cap._BQC = device->cap._BCQ = 0;
665 *level = device->brightness->curr;
666 return 0;
669 static int
670 acpi_video_device_EDID(struct acpi_video_device *device,
671 union acpi_object **edid, ssize_t length)
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 };
680 *edid = NULL;
682 if (!device)
683 return -ENODEV;
684 if (length == 128)
685 arg0.integer.value = 1;
686 else if (length == 256)
687 arg0.integer.value = 2;
688 else
689 return -EINVAL;
691 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
692 if (ACPI_FAILURE(status))
693 return -ENODEV;
695 obj = buffer.pointer;
697 if (obj && obj->type == ACPI_TYPE_BUFFER)
698 *edid = obj;
699 else {
700 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
701 status = -EFAULT;
702 kfree(obj);
705 return status;
708 /* bus */
711 * Arg:
712 * video : video bus device pointer
713 * bios_flag :
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
717 * active display output. No switch event.
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
724 * of the LCD when the power changes from AC to DC
725 * 1. The system BIOS should NOT automatically control the brightness
726 * level of the LCD when the power changes from AC to DC.
727 * Return Value:
728 * -EINVAL wrong arg.
731 static int
732 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
734 acpi_status status;
736 if (!video->cap._DOS)
737 return 0;
739 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
740 return -EINVAL;
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);
744 if (ACPI_FAILURE(status))
745 return -EIO;
747 return 0;
751 * Simple comparison function used to sort backlight levels.
754 static int
755 acpi_video_cmp_level(const void *a, const void *b)
757 return *(int *)a - *(int *)b;
761 * Decides if _BQC/_BCQ for this system is usable
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.
767 static int acpi_video_bqc_quirk(struct acpi_video_device *device,
768 int max_level, int current_level)
770 struct acpi_video_device_brightness *br = device->brightness;
771 int result;
772 unsigned long long level;
773 int test_level;
775 /* don't mess with existing known broken systems */
776 if (bqc_offset_aml_bug_workaround)
777 return 0;
780 * Some systems always report current brightness level as maximum
781 * through _BQC, we need to test another value for them.
783 test_level = current_level == max_level ? br->levels[3] : max_level;
785 result = acpi_video_device_lcd_set_level(device, test_level);
786 if (result)
787 return result;
789 result = acpi_video_device_lcd_get_level_current(device, &level, true);
790 if (result)
791 return result;
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;
802 if (!br->flags._BQC_use_index)
803 device->cap._BQC = device->cap._BCQ = 0;
806 return 0;
811 * Arg:
812 * device : video output device (LCD, CRT, ..)
814 * Return Value:
815 * Maximum brightness level
817 * Allocate and initialize device->brightness.
820 static int
821 acpi_video_init_brightness(struct acpi_video_device *device)
823 union acpi_object *obj = NULL;
824 int i, max_level = 0, count = 0, level_ac_battery = 0;
825 unsigned long long level, level_old;
826 union acpi_object *o;
827 struct acpi_video_device_brightness *br = NULL;
828 int result = -EINVAL;
829 u32 value;
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;
837 if (obj->package.count < 2)
838 goto out;
840 br = kzalloc(sizeof(*br), GFP_KERNEL);
841 if (!br) {
842 printk(KERN_ERR "can't allocate memory\n");
843 result = -ENOMEM;
844 goto out;
847 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
848 GFP_KERNEL);
849 if (!br->levels) {
850 result = -ENOMEM;
851 goto out_free;
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;
860 value = (u32) o->integer.value;
861 /* Skip duplicate entries */
862 if (count > 2 && br->levels[count - 1] == value)
863 continue;
865 br->levels[count] = value;
867 if (br->levels[count] > max_level)
868 max_level = br->levels[count];
869 count++;
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.
878 for (i = 2; i < count; i++) {
879 if (br->levels[i] == br->levels[0])
880 level_ac_battery++;
881 if (br->levels[i] == br->levels[1])
882 level_ac_battery++;
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)
892 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
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,
901 "Found unordered _BCL package"));
903 br->count = count;
904 device->brightness = br;
906 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
907 br->curr = level = max_level;
909 if (!device->cap._BQC)
910 goto set_level;
912 result = acpi_video_device_lcd_get_level_current(device,
913 &level_old, true);
914 if (result)
915 goto out_free_levels;
917 result = acpi_video_bqc_quirk(device, max_level, level_old);
918 if (result)
919 goto out_free_levels;
921 * cap._BQC may get cleared due to _BQC is found to be broken
922 * in acpi_video_bqc_quirk, so check again here.
924 if (!device->cap._BQC)
925 goto set_level;
927 level = acpi_video_bqc_value_to_level(device, level_old);
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.
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;
940 set_level:
941 result = acpi_video_device_lcd_set_level(device, level);
942 if (result)
943 goto out_free_levels;
945 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
946 "found %d brightness levels\n", count - 2));
947 kfree(obj);
948 return result;
950 out_free_levels:
951 kfree(br->levels);
952 out_free:
953 kfree(br);
954 out:
955 device->brightness = NULL;
956 kfree(obj);
957 return result;
961 * Arg:
962 * device : video output device (LCD, CRT, ..)
964 * Return Value:
965 * None
967 * Find out all required AML methods defined under the output
968 * device.
971 static void acpi_video_device_find_cap(struct acpi_video_device *device)
973 if (acpi_has_method(device->dev->handle, "_ADR"))
974 device->cap._ADR = 1;
975 if (acpi_has_method(device->dev->handle, "_BCL"))
976 device->cap._BCL = 1;
977 if (acpi_has_method(device->dev->handle, "_BCM"))
978 device->cap._BCM = 1;
979 if (acpi_has_method(device->dev->handle, "_BQC")) {
980 device->cap._BQC = 1;
981 } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
982 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
983 device->cap._BCQ = 1;
986 if (acpi_has_method(device->dev->handle, "_DDC"))
987 device->cap._DDC = 1;
991 * Arg:
992 * device : video output device (VGA)
994 * Return Value:
995 * None
997 * Find out all required AML methods defined under the video bus device.
1000 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
1002 if (acpi_has_method(video->device->handle, "_DOS"))
1003 video->cap._DOS = 1;
1004 if (acpi_has_method(video->device->handle, "_DOD"))
1005 video->cap._DOD = 1;
1006 if (acpi_has_method(video->device->handle, "_ROM"))
1007 video->cap._ROM = 1;
1008 if (acpi_has_method(video->device->handle, "_GPD"))
1009 video->cap._GPD = 1;
1010 if (acpi_has_method(video->device->handle, "_SPD"))
1011 video->cap._SPD = 1;
1012 if (acpi_has_method(video->device->handle, "_VPO"))
1013 video->cap._VPO = 1;
1017 * Check whether the video bus device has required AML method to
1018 * support the desired features
1021 static int acpi_video_bus_check(struct acpi_video_bus *video)
1023 acpi_status status = -ENOENT;
1024 struct pci_dev *dev;
1026 if (!video)
1027 return -EINVAL;
1029 dev = acpi_get_pci_dev(video->device->handle);
1030 if (!dev)
1031 return -ENODEV;
1032 pci_dev_put(dev);
1035 * Since there is no HID, CID and so on for VGA driver, we have
1036 * to check well known required nodes.
1039 /* Does this device support video switching? */
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));
1046 video->flags.multihead = 1;
1047 status = 0;
1050 /* Does this device support retrieving a video ROM? */
1051 if (video->cap._ROM) {
1052 video->flags.rom = 1;
1053 status = 0;
1056 /* Does this device support configuring which video device to POST? */
1057 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
1058 video->flags.post = 1;
1059 status = 0;
1062 return status;
1066 * --------------------------------------------------------------------------
1067 * Driver Interface
1068 * --------------------------------------------------------------------------
1071 /* device interface */
1072 static struct acpi_video_device_attrib *
1073 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1075 struct acpi_video_enumerated_device *ids;
1076 int i;
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;
1084 return NULL;
1087 static int
1088 acpi_video_get_device_type(struct acpi_video_bus *video,
1089 unsigned long device_id)
1091 struct acpi_video_enumerated_device *ids;
1092 int i;
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;
1100 return 0;
1103 static int
1104 acpi_video_bus_get_one_device(struct acpi_device *device,
1105 struct acpi_video_bus *video)
1107 unsigned long long device_id;
1108 int status, device_type;
1109 struct acpi_video_device *data;
1110 struct acpi_video_device_attrib *attribute;
1112 status =
1113 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1114 /* Some device omits _ADR, we skip them instead of fail */
1115 if (ACPI_FAILURE(status))
1116 return 0;
1118 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1119 if (!data)
1120 return -ENOMEM;
1122 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1123 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1124 device->driver_data = data;
1126 data->device_id = device_id;
1127 data->video = video;
1128 data->dev = device;
1129 INIT_DELAYED_WORK(&data->switch_brightness_work,
1130 acpi_video_switch_brightness);
1132 attribute = acpi_video_get_device_attr(video, device_id);
1134 if (attribute && attribute->device_id_scheme) {
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;
1152 if (attribute->bios_can_detect)
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) {
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;
1173 acpi_video_device_bind(video, data);
1174 acpi_video_device_find_cap(data);
1176 mutex_lock(&video->device_list_lock);
1177 list_add_tail(&data->entry, &video->video_device_list);
1178 mutex_unlock(&video->device_list_lock);
1180 return status;
1184 * Arg:
1185 * video : video bus device
1187 * Return:
1188 * none
1190 * Enumerate the video device list of the video bus,
1191 * bind the ids with the corresponding video devices
1192 * under the video bus.
1195 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1197 struct acpi_video_device *dev;
1199 mutex_lock(&video->device_list_lock);
1201 list_for_each_entry(dev, &video->video_device_list, entry)
1202 acpi_video_device_bind(video, dev);
1204 mutex_unlock(&video->device_list_lock);
1208 * Arg:
1209 * video : video bus device
1210 * device : video output device under the video
1211 * bus
1213 * Return:
1214 * none
1216 * Bind the ids with the corresponding video devices
1217 * under the video bus.
1220 static void
1221 acpi_video_device_bind(struct acpi_video_bus *video,
1222 struct acpi_video_device *device)
1224 struct acpi_video_enumerated_device *ids;
1225 int i;
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;
1231 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1236 static bool acpi_video_device_in_dod(struct acpi_video_device *device)
1238 struct acpi_video_bus *video = device->video;
1239 int i;
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.
1246 if (!video->attached_count || video->child_count > 8)
1247 return true;
1249 for (i = 0; i < video->attached_count; i++) {
1250 if ((video->attached_array[i].value.int_val & 0xfff) ==
1251 (device->device_id & 0xfff))
1252 return true;
1255 return false;
1259 * Arg:
1260 * video : video bus device
1262 * Return:
1263 * < 0 : error
1265 * Call _DOD to enumerate all devices attached to display adapter
1269 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1271 int status;
1272 int count;
1273 int i;
1274 struct acpi_video_enumerated_device *active_list;
1275 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1276 union acpi_object *dod = NULL;
1277 union acpi_object *obj;
1279 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1280 if (!ACPI_SUCCESS(status)) {
1281 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1282 return status;
1285 dod = buffer.pointer;
1286 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1287 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1288 status = -EFAULT;
1289 goto out;
1292 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1293 dod->package.count));
1295 active_list = kcalloc(1 + dod->package.count,
1296 sizeof(struct acpi_video_enumerated_device),
1297 GFP_KERNEL);
1298 if (!active_list) {
1299 status = -ENOMEM;
1300 goto out;
1303 count = 0;
1304 for (i = 0; i < dod->package.count; i++) {
1305 obj = &dod->package.elements[i];
1307 if (obj->type != ACPI_TYPE_INTEGER) {
1308 printk(KERN_ERR PREFIX
1309 "Invalid _DOD data in element %d\n", i);
1310 continue;
1313 active_list[count].value.int_val = obj->integer.value;
1314 active_list[count].bind_info = NULL;
1315 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1316 (int)obj->integer.value));
1317 count++;
1320 kfree(video->attached_array);
1322 video->attached_array = active_list;
1323 video->attached_count = count;
1325 out:
1326 kfree(buffer.pointer);
1327 return status;
1330 static int
1331 acpi_video_get_next_level(struct acpi_video_device *device,
1332 u32 level_current, u32 event)
1334 int min, max, min_above, max_below, i, l, delta = 255;
1335 max = max_below = 0;
1336 min = min_above = 255;
1337 /* Find closest level to level_current */
1338 for (i = 2; i < device->brightness->count; i++) {
1339 l = device->brightness->levels[i];
1340 if (abs(l - level_current) < abs(delta)) {
1341 delta = l - level_current;
1342 if (!delta)
1343 break;
1346 /* Ajust level_current to closest available level */
1347 level_current += delta;
1348 for (i = 2; i < device->brightness->count; i++) {
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;
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;
1375 static void
1376 acpi_video_switch_brightness(struct work_struct *work)
1378 struct acpi_video_device *device = container_of(to_delayed_work(work),
1379 struct acpi_video_device, switch_brightness_work);
1380 unsigned long long level_current, level_next;
1381 int event = device->switch_brightness_event;
1382 int result = -EINVAL;
1384 /* no warning message if acpi_backlight=vendor or a quirk is used */
1385 if (!acpi_video_verify_backlight_support())
1386 return;
1388 if (!device->brightness)
1389 goto out;
1391 result = acpi_video_device_lcd_get_level_current(device,
1392 &level_current,
1393 false);
1394 if (result)
1395 goto out;
1397 level_next = acpi_video_get_next_level(device, level_current, event);
1399 result = acpi_video_device_lcd_set_level(device, level_next);
1401 if (!result)
1402 backlight_force_update(device->backlight,
1403 BACKLIGHT_UPDATE_HOTKEY);
1405 out:
1406 if (result)
1407 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1410 int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1411 void **edid)
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;
1419 if (!device || !acpi_driver_data(device))
1420 return -EINVAL;
1422 video = acpi_driver_data(device);
1424 for (i = 0; i < video->attached_count; i++) {
1425 video_device = video->attached_array[i].bind_info;
1426 length = 256;
1428 if (!video_device)
1429 continue;
1431 if (!video_device->cap._DDC)
1432 continue;
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;
1453 } else if (video_device->device_id != device_id) {
1454 continue;
1457 status = acpi_video_device_EDID(video_device, &buffer, length);
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;
1470 *edid = buffer->buffer.pointer;
1471 return length;
1474 return -ENODEV;
1476 EXPORT_SYMBOL(acpi_video_get_edid);
1478 static int
1479 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1480 struct acpi_device *device)
1482 int status = 0;
1483 struct acpi_device *dev;
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.
1490 acpi_video_device_enumerate(video);
1492 list_for_each_entry(dev, &device->children, node) {
1494 status = acpi_video_bus_get_one_device(dev, video);
1495 if (status) {
1496 dev_err(&dev->dev, "Can't attach device\n");
1497 break;
1499 video->child_count++;
1501 return status;
1504 /* acpi_video interface */
1507 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1508 * preform any automatic brightness change on receiving a notification.
1510 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1512 return acpi_video_bus_DOS(video, 0,
1513 acpi_osi_is_win8() ? 1 : 0);
1516 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1518 return acpi_video_bus_DOS(video, 0,
1519 acpi_osi_is_win8() ? 0 : 1);
1522 static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
1524 struct acpi_video_bus *video = acpi_driver_data(device);
1525 struct input_dev *input;
1526 int keycode = 0;
1528 if (!video || !video->input)
1529 return;
1531 input = video->input;
1533 switch (event) {
1534 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
1535 * most likely via hotkey. */
1536 keycode = KEY_SWITCHVIDEOMODE;
1537 break;
1539 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
1540 * connector. */
1541 acpi_video_device_enumerate(video);
1542 acpi_video_device_rebind(video);
1543 keycode = KEY_SWITCHVIDEOMODE;
1544 break;
1546 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
1547 keycode = KEY_SWITCHVIDEOMODE;
1548 break;
1549 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
1550 keycode = KEY_VIDEO_NEXT;
1551 break;
1552 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
1553 keycode = KEY_VIDEO_PREV;
1554 break;
1556 default:
1557 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1558 "Unsupported event [0x%x]\n", event));
1559 break;
1562 if (acpi_notifier_call_chain(device, event, 0))
1563 /* Something vetoed the keypress. */
1564 keycode = 0;
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);
1573 return;
1576 static void brightness_switch_event(struct acpi_video_device *video_device,
1577 u32 event)
1579 if (!brightness_switch_enabled)
1580 return;
1582 video_device->switch_brightness_event = event;
1583 schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
1586 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1588 struct acpi_video_device *video_device = data;
1589 struct acpi_device *device = NULL;
1590 struct acpi_video_bus *bus;
1591 struct input_dev *input;
1592 int keycode = 0;
1594 if (!video_device)
1595 return;
1597 device = video_device->dev;
1598 bus = video_device->video;
1599 input = bus->input;
1601 switch (event) {
1602 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
1603 brightness_switch_event(video_device, event);
1604 keycode = KEY_BRIGHTNESS_CYCLE;
1605 break;
1606 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
1607 brightness_switch_event(video_device, event);
1608 keycode = KEY_BRIGHTNESSUP;
1609 break;
1610 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
1611 brightness_switch_event(video_device, event);
1612 keycode = KEY_BRIGHTNESSDOWN;
1613 break;
1614 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
1615 brightness_switch_event(video_device, event);
1616 keycode = KEY_BRIGHTNESS_ZERO;
1617 break;
1618 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
1619 brightness_switch_event(video_device, event);
1620 keycode = KEY_DISPLAY_OFF;
1621 break;
1622 default:
1623 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1624 "Unsupported event [0x%x]\n", event));
1625 break;
1628 acpi_notifier_call_chain(device, event, 0);
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);
1637 return;
1640 static int acpi_video_resume(struct notifier_block *nb,
1641 unsigned long val, void *ign)
1643 struct acpi_video_bus *video;
1644 struct acpi_video_device *video_device;
1645 int i;
1647 switch (val) {
1648 case PM_HIBERNATION_PREPARE:
1649 case PM_SUSPEND_PREPARE:
1650 case PM_RESTORE_PREPARE:
1651 return NOTIFY_DONE;
1654 video = container_of(nb, struct acpi_video_bus, pm_nb);
1656 dev_info(&video->device->dev, "Restoring backlight state\n");
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);
1664 return NOTIFY_OK;
1667 static acpi_status
1668 acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1669 void **return_value)
1671 struct acpi_device *device = context;
1672 struct acpi_device *sibling;
1673 int result;
1675 if (handle == device->handle)
1676 return AE_CTRL_TERMINATE;
1678 result = acpi_bus_get_device(handle, &sibling);
1679 if (result)
1680 return AE_OK;
1682 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1683 return AE_ALREADY_EXISTS;
1685 return AE_OK;
1688 static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
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;
1699 * Do not create backlight device for video output
1700 * device that is not in the enumerated list.
1702 if (!acpi_video_device_in_dod(device)) {
1703 dev_dbg(&device->dev->dev, "not in _DOD list, ignore\n");
1704 return;
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++;
1715 acpi_get_parent(device->dev->handle, &acpi_parent);
1717 pdev = acpi_get_pci_dev(acpi_parent);
1718 if (pdev) {
1719 parent = &pdev->dev;
1720 pci_dev_put(pdev);
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;
1736 * Save current brightness level in case we have to restore it
1737 * before acpi_video_device_lcd_set_level() is called next time.
1739 device->backlight->props.brightness =
1740 acpi_video_get_brightness(device->backlight);
1742 device->cooling_dev = thermal_cooling_device_register("LCD",
1743 device->dev, &video_cooling_ops);
1744 if (IS_ERR(device->cooling_dev)) {
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
1751 device->cooling_dev = NULL;
1752 return;
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");
1768 static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
1770 struct acpi_video_device *dev;
1771 union acpi_object *levels;
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);
1778 mutex_unlock(&video->device_list_lock);
1781 static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
1783 struct acpi_video_device *dev;
1785 if (video->backlight_registered)
1786 return 0;
1788 acpi_video_run_bcl_for_osi(video);
1790 if (!acpi_video_verify_backlight_support())
1791 return 0;
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);
1798 video->backlight_registered = true;
1800 video->pm_nb.notifier_call = acpi_video_resume;
1801 video->pm_nb.priority = 0;
1802 return register_pm_notifier(&video->pm_nb);
1805 static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
1807 if (device->backlight) {
1808 backlight_device_unregister(device->backlight);
1809 device->backlight = NULL;
1811 if (device->brightness) {
1812 kfree(device->brightness->levels);
1813 kfree(device->brightness);
1814 device->brightness = NULL;
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;
1824 static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
1826 struct acpi_video_device *dev;
1827 int error;
1829 if (!video->backlight_registered)
1830 return 0;
1832 error = unregister_pm_notifier(&video->pm_nb);
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);
1839 video->backlight_registered = false;
1841 return error;
1844 static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
1846 acpi_status status;
1847 struct acpi_device *adev = device->dev;
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;
1857 static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
1859 struct input_dev *input;
1860 struct acpi_video_device *dev;
1861 int error;
1863 video->input = input = input_allocate_device();
1864 if (!input) {
1865 error = -ENOMEM;
1866 goto out;
1869 error = acpi_video_bus_start_devices(video);
1870 if (error)
1871 goto err_free_input;
1873 snprintf(video->phys, sizeof(video->phys),
1874 "%s/video/input0", acpi_device_hid(video->device));
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);
1891 error = input_register_device(input);
1892 if (error)
1893 goto err_stop_dev;
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);
1900 return 0;
1902 err_stop_dev:
1903 acpi_video_bus_stop_devices(video);
1904 err_free_input:
1905 input_free_device(input);
1906 video->input = NULL;
1907 out:
1908 return error;
1911 static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
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;
1920 static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
1922 struct acpi_video_device *dev;
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);
1929 acpi_video_bus_stop_devices(video);
1930 input_unregister_device(video->input);
1931 video->input = NULL;
1934 static int acpi_video_backlight_notify(struct notifier_block *nb,
1935 unsigned long val, void *bd)
1937 struct backlight_device *backlight = bd;
1938 struct acpi_video_bus *video;
1940 /* acpi_video_verify_backlight_support only cares about raw devices */
1941 if (backlight->props.type != BACKLIGHT_RAW)
1942 return NOTIFY_DONE;
1944 video = container_of(nb, struct acpi_video_bus, backlight_nb);
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;
1956 return NOTIFY_OK;
1959 static int acpi_video_bus_add_backlight_notify_handler(
1960 struct acpi_video_bus *video)
1962 int error;
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;
1970 return error;
1973 static int acpi_video_bus_remove_backlight_notify_handler(
1974 struct acpi_video_bus *video)
1976 if (!video->backlight_notifier_registered)
1977 return 0;
1979 video->backlight_notifier_registered = false;
1981 return backlight_unregister_notifier(&video->backlight_nb);
1984 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1986 struct acpi_video_device *dev, *next;
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);
1993 mutex_unlock(&video->device_list_lock);
1995 return 0;
1998 static int instance;
2000 static int acpi_video_bus_add(struct acpi_device *device)
2002 struct acpi_video_bus *video;
2003 int error;
2004 acpi_status status;
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;
2020 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
2021 if (!video)
2022 return -ENOMEM;
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;
2028 instance++;
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++;
2037 video->device = device;
2038 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2039 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
2040 device->driver_data = video;
2042 acpi_video_bus_find_cap(video);
2043 error = acpi_video_bus_check(video);
2044 if (error)
2045 goto err_free_video;
2047 mutex_init(&video->device_list_lock);
2048 INIT_LIST_HEAD(&video->video_device_list);
2050 error = acpi_video_bus_get_devices(video, device);
2051 if (error)
2052 goto err_put_video;
2054 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
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");
2059 mutex_lock(&video_list_lock);
2060 list_add_tail(&video->entry, &video_bus_head);
2061 mutex_unlock(&video_list_lock);
2063 acpi_video_bus_register_backlight(video);
2064 acpi_video_bus_add_notify_handler(video);
2065 acpi_video_bus_add_backlight_notify_handler(video);
2067 return 0;
2069 err_put_video:
2070 acpi_video_bus_put_devices(video);
2071 kfree(video->attached_array);
2072 err_free_video:
2073 kfree(video);
2074 device->driver_data = NULL;
2076 return error;
2079 static int acpi_video_bus_remove(struct acpi_device *device)
2081 struct acpi_video_bus *video = NULL;
2084 if (!device || !acpi_driver_data(device))
2085 return -EINVAL;
2087 video = acpi_driver_data(device);
2089 acpi_video_bus_remove_backlight_notify_handler(video);
2090 acpi_video_bus_remove_notify_handler(video);
2091 acpi_video_bus_unregister_backlight(video);
2092 acpi_video_bus_put_devices(video);
2094 mutex_lock(&video_list_lock);
2095 list_del(&video->entry);
2096 mutex_unlock(&video_list_lock);
2098 kfree(video->attached_array);
2099 kfree(video);
2101 return 0;
2104 static int __init is_i740(struct pci_dev *dev)
2106 if (dev->device == 0x00D1)
2107 return 1;
2108 if (dev->device == 0x7000)
2109 return 1;
2110 return 0;
2113 static int __init intel_opregion_present(void)
2115 int opregion = 0;
2116 struct pci_dev *dev = NULL;
2117 u32 address;
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;
2124 /* We don't want to poke around undefined i740 registers */
2125 if (is_i740(dev))
2126 continue;
2127 pci_read_config_dword(dev, 0xfc, &address);
2128 if (!address)
2129 continue;
2130 opregion = 1;
2132 return opregion;
2135 int acpi_video_register(void)
2137 int ret;
2139 if (register_count) {
2141 * if the function of acpi_video_register is already called,
2142 * don't register the acpi_vide_bus again and return no error.
2144 return 0;
2147 mutex_init(&video_list_lock);
2148 INIT_LIST_HEAD(&video_bus_head);
2150 ret = acpi_bus_register_driver(&acpi_video_bus);
2151 if (ret)
2152 return ret;
2155 * When the acpi_video_bus is loaded successfully, increase
2156 * the counter reference.
2158 register_count = 1;
2160 return 0;
2162 EXPORT_SYMBOL(acpi_video_register);
2164 void acpi_video_unregister(void)
2166 if (!register_count) {
2168 * If the acpi video bus is already unloaded, don't
2169 * unload it again and return directly.
2171 return;
2173 acpi_bus_unregister_driver(&acpi_video_bus);
2175 register_count = 0;
2177 return;
2179 EXPORT_SYMBOL(acpi_video_unregister);
2181 void acpi_video_unregister_backlight(void)
2183 struct acpi_video_bus *video;
2185 if (!register_count)
2186 return;
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);
2193 EXPORT_SYMBOL(acpi_video_unregister_backlight);
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.
2202 static int __init acpi_video_init(void)
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.
2209 * acpi_video_register() will report -ENODEV later as well due
2210 * to acpi_disabled when i915.ko tries to register itself afterwards.
2212 if (acpi_disabled)
2213 return 0;
2215 dmi_check_system(video_dmi_table);
2217 if (intel_opregion_present())
2218 return 0;
2220 return acpi_video_register();
2223 static void __exit acpi_video_exit(void)
2225 acpi_video_unregister();
2227 return;
2230 module_init(acpi_video_init);
2231 module_exit(acpi_video_exit);