1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * video.c - ACPI Video Driver
5 * Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
6 * Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
7 * Copyright (C) 2006 Thomas Tuttle <linux-kernel@ttuttle.net>
10 #define pr_fmt(fmt) "ACPI: video: " fmt
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/types.h>
16 #include <linux/list.h>
17 #include <linux/mutex.h>
18 #include <linux/input.h>
19 #include <linux/backlight.h>
20 #include <linux/thermal.h>
21 #include <linux/sort.h>
22 #include <linux/pci.h>
23 #include <linux/pci_ids.h>
24 #include <linux/slab.h>
25 #include <linux/dmi.h>
26 #include <linux/suspend.h>
27 #include <linux/acpi.h>
28 #include <acpi/video.h>
29 #include <linux/uaccess.h>
31 #define ACPI_VIDEO_BUS_NAME "Video Bus"
32 #define ACPI_VIDEO_DEVICE_NAME "Video Device"
34 #define MAX_NAME_LEN 20
36 MODULE_AUTHOR("Bruno Ducrot");
37 MODULE_DESCRIPTION("ACPI Video Driver");
38 MODULE_LICENSE("GPL");
40 static bool brightness_switch_enabled = true;
41 module_param(brightness_switch_enabled, bool, 0644);
44 * By default, we don't allow duplicate ACPI video bus devices
45 * under the same VGA controller
47 static bool allow_duplicates;
48 module_param(allow_duplicates, bool, 0644);
50 #define REPORT_OUTPUT_KEY_EVENTS 0x01
51 #define REPORT_BRIGHTNESS_KEY_EVENTS 0x02
52 static int report_key_events = -1;
53 module_param(report_key_events, int, 0644);
54 MODULE_PARM_DESC(report_key_events,
55 "0: none, 1: output changes, 2: brightness changes, 3: all");
57 static int hw_changes_brightness = -1;
58 module_param(hw_changes_brightness, int, 0644);
59 MODULE_PARM_DESC(hw_changes_brightness,
60 "Set this to 1 on buggy hw which changes the brightness itself when "
61 "a hotkey is pressed: -1: auto, 0: normal 1: hw-changes-brightness");
64 * Whether the struct acpi_video_device_attrib::device_id_scheme bit should be
65 * assumed even if not actually set.
67 static bool device_id_scheme = false;
68 module_param(device_id_scheme, bool, 0444);
70 static int only_lcd = -1;
71 module_param(only_lcd, int, 0444);
74 * Display probing is known to take up to 5 seconds, so delay the fallback
75 * backlight registration by 5 seconds + 3 seconds for some extra margin.
77 static int register_backlight_delay = 8;
78 module_param(register_backlight_delay, int, 0444);
79 MODULE_PARM_DESC(register_backlight_delay,
80 "Delay in seconds before doing fallback (non GPU driver triggered) "
81 "backlight registration, set to 0 to disable.");
83 static bool may_report_brightness_keys;
84 static int register_count;
85 static DEFINE_MUTEX(register_count_mutex);
86 static DEFINE_MUTEX(video_list_lock);
87 static LIST_HEAD(video_bus_head);
88 static int acpi_video_bus_add(struct acpi_device *device);
89 static int acpi_video_bus_remove(struct acpi_device *device);
90 static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
91 static void acpi_video_bus_register_backlight_work(struct work_struct *ignored);
92 static DECLARE_DELAYED_WORK(video_bus_register_backlight_work,
93 acpi_video_bus_register_backlight_work);
96 * Indices in the _BCL method response: the first two items are special,
97 * the rest are all supported levels.
99 * See page 575 of the ACPI spec 3.0
101 enum acpi_video_level_idx {
102 ACPI_VIDEO_AC_LEVEL, /* level when machine has full power */
103 ACPI_VIDEO_BATTERY_LEVEL, /* level when machine is on batteries */
104 ACPI_VIDEO_FIRST_LEVEL, /* actual supported levels begin here */
107 static const struct acpi_device_id video_device_ids[] = {
111 MODULE_DEVICE_TABLE(acpi, video_device_ids);
113 static struct acpi_driver acpi_video_bus = {
115 .class = ACPI_VIDEO_CLASS,
116 .ids = video_device_ids,
118 .add = acpi_video_bus_add,
119 .remove = acpi_video_bus_remove,
120 .notify = acpi_video_bus_notify,
124 struct acpi_video_bus_flags {
125 u8 multihead:1; /* can switch video heads */
126 u8 rom:1; /* can retrieve a video rom */
127 u8 post:1; /* can configure the head to */
131 struct acpi_video_bus_cap {
132 u8 _DOS:1; /* Enable/Disable output switching */
133 u8 _DOD:1; /* Enumerate all devices attached to display adapter */
134 u8 _ROM:1; /* Get ROM Data */
135 u8 _GPD:1; /* Get POST Device */
136 u8 _SPD:1; /* Set POST Device */
137 u8 _VPO:1; /* Video POST Options */
141 struct acpi_video_device_attrib {
142 u32 display_index:4; /* A zero-based instance of the Display */
143 u32 display_port_attachment:4; /* This field differentiates the display type */
144 u32 display_type:4; /* Describe the specific type in use */
145 u32 vendor_specific:4; /* Chipset Vendor Specific */
146 u32 bios_can_detect:1; /* BIOS can detect the device */
147 u32 depend_on_vga:1; /* Non-VGA output device whose power is related to
149 u32 pipe_id:3; /* For VGA multiple-head devices. */
150 u32 reserved:10; /* Must be 0 */
153 * The device ID might not actually follow the scheme described by this
154 * struct acpi_video_device_attrib. If it does, then this bit
155 * device_id_scheme is set; otherwise, other fields should be ignored.
157 * (but also see the global flag device_id_scheme)
159 u32 device_id_scheme:1;
162 struct acpi_video_enumerated_device {
165 struct acpi_video_device_attrib attrib;
167 struct acpi_video_device *bind_info;
170 struct acpi_video_bus {
171 struct acpi_device *device;
172 bool backlight_registered;
174 struct acpi_video_enumerated_device *attached_array;
177 struct acpi_video_bus_cap cap;
178 struct acpi_video_bus_flags flags;
179 struct list_head video_device_list;
180 struct mutex device_list_lock; /* protects video_device_list */
181 struct list_head entry;
182 struct input_dev *input;
183 char phys[32]; /* for input device */
184 struct notifier_block pm_nb;
187 struct acpi_video_device_flags {
198 struct acpi_video_device_cap {
199 u8 _ADR:1; /* Return the unique ID */
200 u8 _BCL:1; /* Query list of brightness control levels supported */
201 u8 _BCM:1; /* Set the brightness level */
202 u8 _BQC:1; /* Get current brightness level */
203 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
204 u8 _DDC:1; /* Return the EDID for this device */
207 struct acpi_video_device {
208 unsigned long device_id;
209 struct acpi_video_device_flags flags;
210 struct acpi_video_device_cap cap;
211 struct list_head entry;
212 struct delayed_work switch_brightness_work;
213 int switch_brightness_event;
214 struct acpi_video_bus *video;
215 struct acpi_device *dev;
216 struct acpi_video_device_brightness *brightness;
217 struct backlight_device *backlight;
218 struct thermal_cooling_device *cooling_dev;
221 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
222 static void acpi_video_device_rebind(struct acpi_video_bus *video);
223 static void acpi_video_device_bind(struct acpi_video_bus *video,
224 struct acpi_video_device *device);
225 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
226 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
228 static int acpi_video_device_lcd_get_level_current(
229 struct acpi_video_device *device,
230 unsigned long long *level, bool raw);
231 static int acpi_video_get_next_level(struct acpi_video_device *device,
232 u32 level_current, u32 event);
233 static void acpi_video_switch_brightness(struct work_struct *work);
235 /* backlight device sysfs support */
236 static int acpi_video_get_brightness(struct backlight_device *bd)
238 unsigned long long cur_level;
240 struct acpi_video_device *vd = bl_get_data(bd);
242 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
244 for (i = ACPI_VIDEO_FIRST_LEVEL; i < vd->brightness->count; i++) {
245 if (vd->brightness->levels[i] == cur_level)
246 return i - ACPI_VIDEO_FIRST_LEVEL;
251 static int acpi_video_set_brightness(struct backlight_device *bd)
253 int request_level = bd->props.brightness + ACPI_VIDEO_FIRST_LEVEL;
254 struct acpi_video_device *vd = bl_get_data(bd);
256 cancel_delayed_work(&vd->switch_brightness_work);
257 return acpi_video_device_lcd_set_level(vd,
258 vd->brightness->levels[request_level]);
261 static const struct backlight_ops acpi_backlight_ops = {
262 .get_brightness = acpi_video_get_brightness,
263 .update_status = acpi_video_set_brightness,
266 /* thermal cooling device callbacks */
267 static int video_get_max_state(struct thermal_cooling_device *cooling_dev,
268 unsigned long *state)
270 struct acpi_device *device = cooling_dev->devdata;
271 struct acpi_video_device *video = acpi_driver_data(device);
273 *state = video->brightness->count - ACPI_VIDEO_FIRST_LEVEL - 1;
277 static int video_get_cur_state(struct thermal_cooling_device *cooling_dev,
278 unsigned long *state)
280 struct acpi_device *device = cooling_dev->devdata;
281 struct acpi_video_device *video = acpi_driver_data(device);
282 unsigned long long level;
285 if (acpi_video_device_lcd_get_level_current(video, &level, false))
287 for (offset = ACPI_VIDEO_FIRST_LEVEL; offset < video->brightness->count;
289 if (level == video->brightness->levels[offset]) {
290 *state = video->brightness->count - offset - 1;
298 video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
300 struct acpi_device *device = cooling_dev->devdata;
301 struct acpi_video_device *video = acpi_driver_data(device);
304 if (state >= video->brightness->count - ACPI_VIDEO_FIRST_LEVEL)
307 state = video->brightness->count - state;
308 level = video->brightness->levels[state - 1];
309 return acpi_video_device_lcd_set_level(video, level);
312 static const struct thermal_cooling_device_ops video_cooling_ops = {
313 .get_max_state = video_get_max_state,
314 .get_cur_state = video_get_cur_state,
315 .set_cur_state = video_set_cur_state,
319 * --------------------------------------------------------------------------
321 * --------------------------------------------------------------------------
325 acpi_video_device_lcd_query_levels(acpi_handle handle,
326 union acpi_object **levels)
329 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
330 union acpi_object *obj;
335 status = acpi_evaluate_object(handle, "_BCL", NULL, &buffer);
336 if (ACPI_FAILURE(status))
338 obj = (union acpi_object *)buffer.pointer;
339 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
340 acpi_handle_info(handle, "Invalid _BCL data\n");
350 kfree(buffer.pointer);
356 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
361 status = acpi_execute_simple_method(device->dev->handle,
363 if (ACPI_FAILURE(status)) {
364 acpi_handle_info(device->dev->handle, "_BCM evaluation failed\n");
368 device->brightness->curr = level;
369 for (state = ACPI_VIDEO_FIRST_LEVEL; state < device->brightness->count;
371 if (level == device->brightness->levels[state]) {
372 if (device->backlight)
373 device->backlight->props.brightness =
374 state - ACPI_VIDEO_FIRST_LEVEL;
378 acpi_handle_info(device->dev->handle, "Current brightness invalid\n");
383 * For some buggy _BQC methods, we need to add a constant value to
384 * the _BQC return value to get the actual current brightness level
387 static int bqc_offset_aml_bug_workaround;
388 static int video_set_bqc_offset(const struct dmi_system_id *d)
390 bqc_offset_aml_bug_workaround = 9;
394 static int video_set_device_id_scheme(const struct dmi_system_id *d)
396 device_id_scheme = true;
400 static int video_enable_only_lcd(const struct dmi_system_id *d)
406 static int video_set_report_key_events(const struct dmi_system_id *id)
408 if (report_key_events == -1)
409 report_key_events = (uintptr_t)id->driver_data;
413 static int video_hw_changes_brightness(
414 const struct dmi_system_id *d)
416 if (hw_changes_brightness == -1)
417 hw_changes_brightness = 1;
421 static const struct dmi_system_id video_dmi_table[] = {
423 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
426 .callback = video_set_bqc_offset,
427 .ident = "Acer Aspire 5720",
429 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
430 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
434 .callback = video_set_bqc_offset,
435 .ident = "Acer Aspire 5710Z",
437 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
438 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
442 .callback = video_set_bqc_offset,
443 .ident = "eMachines E510",
445 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
446 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
450 .callback = video_set_bqc_offset,
451 .ident = "Acer Aspire 5315",
453 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
454 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
458 .callback = video_set_bqc_offset,
459 .ident = "Acer Aspire 7720",
461 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
462 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
467 * Some machine's _DOD IDs don't have bit 31(Device ID Scheme) set
468 * but the IDs actually follow the Device ID Scheme.
471 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
472 .callback = video_set_device_id_scheme,
473 .ident = "ESPRIMO Mobile M9410",
475 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
476 DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"),
480 * Some machines have multiple video output devices, but only the one
481 * that is the type of LCD can do the backlight control so we should not
482 * register backlight interface for other video output devices.
485 /* https://bugzilla.kernel.org/show_bug.cgi?id=104121 */
486 .callback = video_enable_only_lcd,
487 .ident = "ESPRIMO Mobile M9410",
489 DMI_MATCH(DMI_SYS_VENDOR, "FUJITSU SIEMENS"),
490 DMI_MATCH(DMI_PRODUCT_NAME, "ESPRIMO Mobile M9410"),
494 * Some machines report wrong key events on the acpi-bus, suppress
495 * key event reporting on these. Note this is only intended to work
496 * around events which are plain wrong. In some cases we get double
497 * events, in this case acpi-video is considered the canonical source
498 * and the events from the other source should be filtered. E.g.
499 * by calling acpi_video_handles_brightness_key_presses() from the
500 * vendor acpi/wmi driver or by using /lib/udev/hwdb.d/60-keyboard.hwdb
503 .callback = video_set_report_key_events,
504 .driver_data = (void *)((uintptr_t)REPORT_OUTPUT_KEY_EVENTS),
505 .ident = "Dell Vostro V131",
507 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
508 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro V131"),
512 .callback = video_set_report_key_events,
513 .driver_data = (void *)((uintptr_t)REPORT_BRIGHTNESS_KEY_EVENTS),
514 .ident = "Dell Vostro 3350",
516 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
517 DMI_MATCH(DMI_PRODUCT_NAME, "Vostro 3350"),
521 * Some machines change the brightness themselves when a brightness
522 * hotkey gets pressed, despite us telling them not to. In this case
523 * acpi_video_device_notify() should only call backlight_force_update(
524 * BACKLIGHT_UPDATE_HOTKEY) and not do anything else.
527 /* https://bugzilla.kernel.org/show_bug.cgi?id=204077 */
528 .callback = video_hw_changes_brightness,
529 .ident = "Packard Bell EasyNote MZ35",
531 DMI_MATCH(DMI_SYS_VENDOR, "Packard Bell"),
532 DMI_MATCH(DMI_PRODUCT_NAME, "EasyNote MZ35"),
538 static unsigned long long
539 acpi_video_bqc_value_to_level(struct acpi_video_device *device,
540 unsigned long long bqc_value)
542 unsigned long long level;
544 if (device->brightness->flags._BQC_use_index) {
546 * _BQC returns an index that doesn't account for the first 2
547 * items with special meaning (see enum acpi_video_level_idx),
548 * so we need to compensate for that by offsetting ourselves
550 if (device->brightness->flags._BCL_reversed)
551 bqc_value = device->brightness->count -
552 ACPI_VIDEO_FIRST_LEVEL - 1 - bqc_value;
554 level = device->brightness->levels[bqc_value +
555 ACPI_VIDEO_FIRST_LEVEL];
560 level += bqc_offset_aml_bug_workaround;
566 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
567 unsigned long long *level, bool raw)
569 acpi_status status = AE_OK;
572 if (device->cap._BQC || device->cap._BCQ) {
573 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
575 status = acpi_evaluate_integer(device->dev->handle, buf,
577 if (ACPI_SUCCESS(status)) {
580 * Caller has indicated he wants the raw
581 * value returned by _BQC, so don't furtherly
582 * mess with the value.
587 *level = acpi_video_bqc_value_to_level(device, *level);
589 for (i = ACPI_VIDEO_FIRST_LEVEL;
590 i < device->brightness->count; i++)
591 if (device->brightness->levels[i] == *level) {
592 device->brightness->curr = *level;
596 * BQC returned an invalid level.
599 acpi_handle_info(device->dev->handle,
600 "%s returned an invalid level", buf);
601 device->cap._BQC = device->cap._BCQ = 0;
605 * should we return an error or ignore this failure?
606 * dev->brightness->curr is a cached value which stores
607 * the correct current backlight level in most cases.
608 * ACPI video backlight still works w/ buggy _BQC.
609 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
611 acpi_handle_info(device->dev->handle,
612 "%s evaluation failed", buf);
613 device->cap._BQC = device->cap._BCQ = 0;
617 *level = device->brightness->curr;
622 acpi_video_device_EDID(struct acpi_video_device *device,
623 union acpi_object **edid, ssize_t length)
626 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
627 union acpi_object *obj;
628 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
629 struct acpi_object_list args = { 1, &arg0 };
637 arg0.integer.value = 1;
638 else if (length == 256)
639 arg0.integer.value = 2;
643 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
644 if (ACPI_FAILURE(status))
647 obj = buffer.pointer;
649 if (obj && obj->type == ACPI_TYPE_BUFFER)
652 acpi_handle_info(device->dev->handle, "Invalid _DDC data\n");
664 * video : video bus device pointer
666 * 0. The system BIOS should NOT automatically switch(toggle)
667 * the active display output.
668 * 1. The system BIOS should automatically switch (toggle) the
669 * active display output. No switch event.
670 * 2. The _DGS value should be locked.
671 * 3. The system BIOS should not automatically switch (toggle) the
672 * active display output, but instead generate the display switch
675 * 0. The system BIOS should automatically control the brightness level
677 * - the power changes from AC to DC (ACPI appendix B)
678 * - a brightness hotkey gets pressed (implied by Win7/8 backlight docs)
679 * 1. The system BIOS should NOT automatically control the brightness
680 * level of the LCD when:
681 * - the power changes from AC to DC (ACPI appendix B)
682 * - a brightness hotkey gets pressed (implied by Win7/8 backlight docs)
688 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
692 if (!video->cap._DOS)
695 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
697 video->dos_setting = (lcd_flag << 2) | bios_flag;
698 status = acpi_execute_simple_method(video->device->handle, "_DOS",
699 (lcd_flag << 2) | bios_flag);
700 if (ACPI_FAILURE(status))
707 * Simple comparison function used to sort backlight levels.
711 acpi_video_cmp_level(const void *a, const void *b)
713 return *(int *)a - *(int *)b;
717 * Decides if _BQC/_BCQ for this system is usable
719 * We do this by changing the level first and then read out the current
720 * brightness level, if the value does not match, find out if it is using
721 * index. If not, clear the _BQC/_BCQ capability.
723 static int acpi_video_bqc_quirk(struct acpi_video_device *device,
724 int max_level, int current_level)
726 struct acpi_video_device_brightness *br = device->brightness;
728 unsigned long long level;
731 /* don't mess with existing known broken systems */
732 if (bqc_offset_aml_bug_workaround)
736 * Some systems always report current brightness level as maximum
737 * through _BQC, we need to test another value for them. However,
738 * there is a subtlety:
740 * If the _BCL package ordering is descending, the first level
741 * (br->levels[2]) is likely to be 0, and if the number of levels
742 * matches the number of steps, we might confuse a returned level to
747 * current_level = max_level = 100
749 * returned level = 100
751 * In this case 100 means the level, not the index, and _BCM failed.
752 * Still, if the _BCL package ordering is descending, the index of
753 * level 0 is also 100, so we assume _BQC is indexed, when it's not.
755 * This causes all _BQC calls to return bogus values causing weird
756 * behavior from the user's perspective. For example:
758 * xbacklight -set 10; xbacklight -set 20;
760 * would flash to 90% and then slowly down to the desired level (20).
762 * The solution is simple; test anything other than the first level
765 test_level = current_level == max_level
766 ? br->levels[ACPI_VIDEO_FIRST_LEVEL + 1]
769 result = acpi_video_device_lcd_set_level(device, test_level);
773 result = acpi_video_device_lcd_get_level_current(device, &level, true);
777 if (level != test_level) {
778 /* buggy _BQC found, need to find out if it uses index */
779 if (level < br->count) {
780 if (br->flags._BCL_reversed)
781 level = br->count - ACPI_VIDEO_FIRST_LEVEL - 1 - level;
782 if (br->levels[level + ACPI_VIDEO_FIRST_LEVEL] == test_level)
783 br->flags._BQC_use_index = 1;
786 if (!br->flags._BQC_use_index)
787 device->cap._BQC = device->cap._BCQ = 0;
793 int acpi_video_get_levels(struct acpi_device *device,
794 struct acpi_video_device_brightness **dev_br,
797 union acpi_object *obj = NULL;
798 int i, max_level = 0, count = 0, level_ac_battery = 0;
799 union acpi_object *o;
800 struct acpi_video_device_brightness *br = NULL;
804 if (ACPI_FAILURE(acpi_video_device_lcd_query_levels(device->handle, &obj))) {
805 acpi_handle_debug(device->handle,
806 "Could not query available LCD brightness level\n");
811 if (obj->package.count < ACPI_VIDEO_FIRST_LEVEL) {
816 br = kzalloc(sizeof(*br), GFP_KERNEL);
823 * Note that we have to reserve 2 extra items (ACPI_VIDEO_FIRST_LEVEL),
824 * in order to account for buggy BIOS which don't export the first two
825 * special levels (see below)
827 br->levels = kmalloc_array(obj->package.count + ACPI_VIDEO_FIRST_LEVEL,
835 for (i = 0; i < obj->package.count; i++) {
836 o = (union acpi_object *)&obj->package.elements[i];
837 if (o->type != ACPI_TYPE_INTEGER) {
838 acpi_handle_info(device->handle, "Invalid data\n");
841 value = (u32) o->integer.value;
842 /* Skip duplicate entries */
843 if (count > ACPI_VIDEO_FIRST_LEVEL
844 && br->levels[count - 1] == value)
847 br->levels[count] = value;
849 if (br->levels[count] > max_level)
850 max_level = br->levels[count];
855 * some buggy BIOS don't export the levels
856 * when machine is on AC/Battery in _BCL package.
857 * In this case, the first two elements in _BCL packages
858 * are also supported brightness levels that OS should take care of.
860 for (i = ACPI_VIDEO_FIRST_LEVEL; i < count; i++) {
861 if (br->levels[i] == br->levels[ACPI_VIDEO_AC_LEVEL])
863 if (br->levels[i] == br->levels[ACPI_VIDEO_BATTERY_LEVEL])
867 if (level_ac_battery < ACPI_VIDEO_FIRST_LEVEL) {
868 level_ac_battery = ACPI_VIDEO_FIRST_LEVEL - level_ac_battery;
869 br->flags._BCL_no_ac_battery_levels = 1;
870 for (i = (count - 1 + level_ac_battery);
871 i >= ACPI_VIDEO_FIRST_LEVEL; i--)
872 br->levels[i] = br->levels[i - level_ac_battery];
873 count += level_ac_battery;
874 } else if (level_ac_battery > ACPI_VIDEO_FIRST_LEVEL)
875 acpi_handle_info(device->handle,
876 "Too many duplicates in _BCL package");
878 /* Check if the _BCL package is in a reversed order */
879 if (max_level == br->levels[ACPI_VIDEO_FIRST_LEVEL]) {
880 br->flags._BCL_reversed = 1;
881 sort(&br->levels[ACPI_VIDEO_FIRST_LEVEL],
882 count - ACPI_VIDEO_FIRST_LEVEL,
883 sizeof(br->levels[ACPI_VIDEO_FIRST_LEVEL]),
884 acpi_video_cmp_level, NULL);
885 } else if (max_level != br->levels[count - 1])
886 acpi_handle_info(device->handle,
887 "Found unordered _BCL package");
892 *pmax_level = max_level;
901 EXPORT_SYMBOL(acpi_video_get_levels);
905 * device : video output device (LCD, CRT, ..)
908 * Maximum brightness level
910 * Allocate and initialize device->brightness.
914 acpi_video_init_brightness(struct acpi_video_device *device)
916 int i, max_level = 0;
917 unsigned long long level, level_old;
918 struct acpi_video_device_brightness *br = NULL;
921 result = acpi_video_get_levels(device->dev, &br, &max_level);
924 device->brightness = br;
926 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
927 br->curr = level = max_level;
929 if (!device->cap._BQC)
932 result = acpi_video_device_lcd_get_level_current(device,
935 goto out_free_levels;
937 result = acpi_video_bqc_quirk(device, max_level, level_old);
939 goto out_free_levels;
941 * cap._BQC may get cleared due to _BQC is found to be broken
942 * in acpi_video_bqc_quirk, so check again here.
944 if (!device->cap._BQC)
947 level = acpi_video_bqc_value_to_level(device, level_old);
949 * On some buggy laptops, _BQC returns an uninitialized
950 * value when invoked for the first time, i.e.
951 * level_old is invalid (no matter whether it's a level
952 * or an index). Set the backlight to max_level in this case.
954 for (i = ACPI_VIDEO_FIRST_LEVEL; i < br->count; i++)
955 if (level == br->levels[i])
957 if (i == br->count || !level)
961 result = acpi_video_device_lcd_set_level(device, level);
963 goto out_free_levels;
965 acpi_handle_debug(device->dev->handle, "found %d brightness levels\n",
966 br->count - ACPI_VIDEO_FIRST_LEVEL);
973 device->brightness = NULL;
979 * device : video output device (LCD, CRT, ..)
984 * Find out all required AML methods defined under the output
988 static void acpi_video_device_find_cap(struct acpi_video_device *device)
990 if (acpi_has_method(device->dev->handle, "_ADR"))
991 device->cap._ADR = 1;
992 if (acpi_has_method(device->dev->handle, "_BCL"))
993 device->cap._BCL = 1;
994 if (acpi_has_method(device->dev->handle, "_BCM"))
995 device->cap._BCM = 1;
996 if (acpi_has_method(device->dev->handle, "_BQC")) {
997 device->cap._BQC = 1;
998 } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
999 acpi_handle_info(device->dev->handle,
1000 "_BCQ is used instead of _BQC\n");
1001 device->cap._BCQ = 1;
1004 if (acpi_has_method(device->dev->handle, "_DDC"))
1005 device->cap._DDC = 1;
1010 * device : video output device (VGA)
1015 * Find out all required AML methods defined under the video bus device.
1018 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
1020 if (acpi_has_method(video->device->handle, "_DOS"))
1021 video->cap._DOS = 1;
1022 if (acpi_has_method(video->device->handle, "_DOD"))
1023 video->cap._DOD = 1;
1024 if (acpi_has_method(video->device->handle, "_ROM"))
1025 video->cap._ROM = 1;
1026 if (acpi_has_method(video->device->handle, "_GPD"))
1027 video->cap._GPD = 1;
1028 if (acpi_has_method(video->device->handle, "_SPD"))
1029 video->cap._SPD = 1;
1030 if (acpi_has_method(video->device->handle, "_VPO"))
1031 video->cap._VPO = 1;
1035 * Check whether the video bus device has required AML method to
1036 * support the desired features
1039 static int acpi_video_bus_check(struct acpi_video_bus *video)
1041 acpi_status status = -ENOENT;
1042 struct pci_dev *dev;
1047 dev = acpi_get_pci_dev(video->device->handle);
1053 * Since there is no HID, CID and so on for VGA driver, we have
1054 * to check well known required nodes.
1057 /* Does this device support video switching? */
1058 if (video->cap._DOS || video->cap._DOD) {
1059 if (!video->cap._DOS) {
1060 pr_info(FW_BUG "ACPI(%s) defines _DOD but not _DOS\n",
1061 acpi_device_bid(video->device));
1063 video->flags.multihead = 1;
1067 /* Does this device support retrieving a video ROM? */
1068 if (video->cap._ROM) {
1069 video->flags.rom = 1;
1073 /* Does this device support configuring which video device to POST? */
1074 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
1075 video->flags.post = 1;
1083 * --------------------------------------------------------------------------
1085 * --------------------------------------------------------------------------
1088 /* device interface */
1089 static struct acpi_video_device_attrib *
1090 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1092 struct acpi_video_enumerated_device *ids;
1095 for (i = 0; i < video->attached_count; i++) {
1096 ids = &video->attached_array[i];
1097 if ((ids->value.int_val & 0xffff) == device_id)
1098 return &ids->value.attrib;
1105 acpi_video_get_device_type(struct acpi_video_bus *video,
1106 unsigned long device_id)
1108 struct acpi_video_enumerated_device *ids;
1111 for (i = 0; i < video->attached_count; i++) {
1112 ids = &video->attached_array[i];
1113 if ((ids->value.int_val & 0xffff) == device_id)
1114 return ids->value.int_val;
1120 static int acpi_video_bus_get_one_device(struct acpi_device *device, void *arg)
1122 struct acpi_video_bus *video = arg;
1123 struct acpi_video_device_attrib *attribute;
1124 struct acpi_video_device *data;
1125 unsigned long long device_id;
1129 status = acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1130 /* Skip devices without _ADR instead of failing. */
1131 if (ACPI_FAILURE(status))
1134 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1136 dev_dbg(&device->dev, "Cannot attach\n");
1140 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1141 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1142 device->driver_data = data;
1144 data->device_id = device_id;
1145 data->video = video;
1147 INIT_DELAYED_WORK(&data->switch_brightness_work,
1148 acpi_video_switch_brightness);
1150 attribute = acpi_video_get_device_attr(video, device_id);
1152 if (attribute && (attribute->device_id_scheme || device_id_scheme)) {
1153 switch (attribute->display_type) {
1154 case ACPI_VIDEO_DISPLAY_CRT:
1155 data->flags.crt = 1;
1157 case ACPI_VIDEO_DISPLAY_TV:
1158 data->flags.tvout = 1;
1160 case ACPI_VIDEO_DISPLAY_DVI:
1161 data->flags.dvi = 1;
1163 case ACPI_VIDEO_DISPLAY_LCD:
1164 data->flags.lcd = 1;
1167 data->flags.unknown = 1;
1170 if (attribute->bios_can_detect)
1171 data->flags.bios = 1;
1173 /* Check for legacy IDs */
1174 device_type = acpi_video_get_device_type(video, device_id);
1175 /* Ignore bits 16 and 18-20 */
1176 switch (device_type & 0xffe2ffff) {
1177 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1178 data->flags.crt = 1;
1180 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1181 data->flags.lcd = 1;
1183 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1184 data->flags.tvout = 1;
1187 data->flags.unknown = 1;
1191 acpi_video_device_bind(video, data);
1192 acpi_video_device_find_cap(data);
1194 if (data->cap._BCM && data->cap._BCL)
1195 may_report_brightness_keys = true;
1197 mutex_lock(&video->device_list_lock);
1198 list_add_tail(&data->entry, &video->video_device_list);
1199 mutex_unlock(&video->device_list_lock);
1202 video->child_count++;
1208 * video : video bus device
1213 * Enumerate the video device list of the video bus,
1214 * bind the ids with the corresponding video devices
1215 * under the video bus.
1218 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1220 struct acpi_video_device *dev;
1222 mutex_lock(&video->device_list_lock);
1224 list_for_each_entry(dev, &video->video_device_list, entry)
1225 acpi_video_device_bind(video, dev);
1227 mutex_unlock(&video->device_list_lock);
1232 * video : video bus device
1233 * device : video output device under the video
1239 * Bind the ids with the corresponding video devices
1240 * under the video bus.
1244 acpi_video_device_bind(struct acpi_video_bus *video,
1245 struct acpi_video_device *device)
1247 struct acpi_video_enumerated_device *ids;
1250 for (i = 0; i < video->attached_count; i++) {
1251 ids = &video->attached_array[i];
1252 if (device->device_id == (ids->value.int_val & 0xffff)) {
1253 ids->bind_info = device;
1254 acpi_handle_debug(video->device->handle, "%s: %d\n",
1260 static bool acpi_video_device_in_dod(struct acpi_video_device *device)
1262 struct acpi_video_bus *video = device->video;
1266 * If we have a broken _DOD or we have more than 8 output devices
1267 * under the graphics controller node that we can't proper deal with
1268 * in the operation region code currently, no need to test.
1270 if (!video->attached_count || video->child_count > 8)
1273 for (i = 0; i < video->attached_count; i++) {
1274 if ((video->attached_array[i].value.int_val & 0xfff) ==
1275 (device->device_id & 0xfff))
1284 * video : video bus device
1289 * Call _DOD to enumerate all devices attached to display adapter
1293 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1298 struct acpi_video_enumerated_device *active_list;
1299 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1300 union acpi_object *dod = NULL;
1301 union acpi_object *obj;
1303 if (!video->cap._DOD)
1304 return AE_NOT_EXIST;
1306 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1307 if (ACPI_FAILURE(status)) {
1308 acpi_handle_info(video->device->handle,
1309 "_DOD evaluation failed: %s\n",
1310 acpi_format_exception(status));
1314 dod = buffer.pointer;
1315 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1316 acpi_handle_info(video->device->handle, "Invalid _DOD data\n");
1321 acpi_handle_debug(video->device->handle, "Found %d video heads in _DOD\n",
1322 dod->package.count);
1324 active_list = kcalloc(1 + dod->package.count,
1325 sizeof(struct acpi_video_enumerated_device),
1333 for (i = 0; i < dod->package.count; i++) {
1334 obj = &dod->package.elements[i];
1336 if (obj->type != ACPI_TYPE_INTEGER) {
1337 acpi_handle_info(video->device->handle,
1338 "Invalid _DOD data in element %d\n", i);
1342 active_list[count].value.int_val = obj->integer.value;
1343 active_list[count].bind_info = NULL;
1345 acpi_handle_debug(video->device->handle,
1346 "_DOD element[%d] = %d\n", i,
1347 (int)obj->integer.value);
1352 kfree(video->attached_array);
1354 video->attached_array = active_list;
1355 video->attached_count = count;
1358 kfree(buffer.pointer);
1363 acpi_video_get_next_level(struct acpi_video_device *device,
1364 u32 level_current, u32 event)
1366 int min, max, min_above, max_below, i, l, delta = 255;
1367 max = max_below = 0;
1368 min = min_above = 255;
1369 /* Find closest level to level_current */
1370 for (i = ACPI_VIDEO_FIRST_LEVEL; i < device->brightness->count; i++) {
1371 l = device->brightness->levels[i];
1372 if (abs(l - level_current) < abs(delta)) {
1373 delta = l - level_current;
1378 /* Adjust level_current to closest available level */
1379 level_current += delta;
1380 for (i = ACPI_VIDEO_FIRST_LEVEL; i < device->brightness->count; i++) {
1381 l = device->brightness->levels[i];
1386 if (l < min_above && l > level_current)
1388 if (l > max_below && l < level_current)
1393 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1394 return (level_current < max) ? min_above : min;
1395 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1396 return (level_current < max) ? min_above : max;
1397 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1398 return (level_current > min) ? max_below : min;
1399 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1400 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1403 return level_current;
1408 acpi_video_switch_brightness(struct work_struct *work)
1410 struct acpi_video_device *device = container_of(to_delayed_work(work),
1411 struct acpi_video_device, switch_brightness_work);
1412 unsigned long long level_current, level_next;
1413 int event = device->switch_brightness_event;
1414 int result = -EINVAL;
1416 /* no warning message if acpi_backlight=vendor or a quirk is used */
1417 if (!device->backlight)
1420 if (!device->brightness)
1423 result = acpi_video_device_lcd_get_level_current(device,
1429 level_next = acpi_video_get_next_level(device, level_current, event);
1431 result = acpi_video_device_lcd_set_level(device, level_next);
1434 backlight_force_update(device->backlight,
1435 BACKLIGHT_UPDATE_HOTKEY);
1439 acpi_handle_info(device->dev->handle,
1440 "Failed to switch brightness\n");
1443 int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1446 struct acpi_video_bus *video;
1447 struct acpi_video_device *video_device;
1448 union acpi_object *buffer = NULL;
1452 if (!device || !acpi_driver_data(device))
1455 video = acpi_driver_data(device);
1457 for (i = 0; i < video->attached_count; i++) {
1458 video_device = video->attached_array[i].bind_info;
1464 if (!video_device->cap._DDC)
1469 case ACPI_VIDEO_DISPLAY_CRT:
1470 if (!video_device->flags.crt)
1473 case ACPI_VIDEO_DISPLAY_TV:
1474 if (!video_device->flags.tvout)
1477 case ACPI_VIDEO_DISPLAY_DVI:
1478 if (!video_device->flags.dvi)
1481 case ACPI_VIDEO_DISPLAY_LCD:
1482 if (!video_device->flags.lcd)
1486 } else if (video_device->device_id != device_id) {
1490 status = acpi_video_device_EDID(video_device, &buffer, length);
1492 if (ACPI_FAILURE(status) || !buffer ||
1493 buffer->type != ACPI_TYPE_BUFFER) {
1495 status = acpi_video_device_EDID(video_device, &buffer,
1497 if (ACPI_FAILURE(status) || !buffer ||
1498 buffer->type != ACPI_TYPE_BUFFER) {
1503 *edid = buffer->buffer.pointer;
1509 EXPORT_SYMBOL(acpi_video_get_edid);
1512 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1513 struct acpi_device *device)
1516 * There are systems where video module known to work fine regardless
1517 * of broken _DOD and ignoring returned value here doesn't cause
1520 acpi_video_device_enumerate(video);
1522 return acpi_dev_for_each_child(device, acpi_video_bus_get_one_device, video);
1525 /* acpi_video interface */
1528 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1529 * perform any automatic brightness change on receiving a notification.
1531 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1533 return acpi_video_bus_DOS(video, 0,
1534 acpi_osi_is_win8() ? 1 : 0);
1537 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1539 return acpi_video_bus_DOS(video, 0,
1540 acpi_osi_is_win8() ? 0 : 1);
1543 static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
1545 struct acpi_video_bus *video = acpi_driver_data(device);
1546 struct input_dev *input;
1549 if (!video || !video->input)
1552 input = video->input;
1555 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
1556 * most likely via hotkey. */
1557 keycode = KEY_SWITCHVIDEOMODE;
1560 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
1562 acpi_video_device_enumerate(video);
1563 acpi_video_device_rebind(video);
1564 keycode = KEY_SWITCHVIDEOMODE;
1567 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
1568 keycode = KEY_SWITCHVIDEOMODE;
1570 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
1571 keycode = KEY_VIDEO_NEXT;
1573 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
1574 keycode = KEY_VIDEO_PREV;
1578 acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
1583 if (acpi_notifier_call_chain(device, event, 0))
1584 /* Something vetoed the keypress. */
1587 if (keycode && (report_key_events & REPORT_OUTPUT_KEY_EVENTS)) {
1588 input_report_key(input, keycode, 1);
1590 input_report_key(input, keycode, 0);
1595 static void brightness_switch_event(struct acpi_video_device *video_device,
1598 if (!brightness_switch_enabled)
1601 video_device->switch_brightness_event = event;
1602 schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
1605 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1607 struct acpi_video_device *video_device = data;
1608 struct acpi_device *device = NULL;
1609 struct acpi_video_bus *bus;
1610 struct input_dev *input;
1616 device = video_device->dev;
1617 bus = video_device->video;
1620 if (hw_changes_brightness > 0) {
1621 if (video_device->backlight)
1622 backlight_force_update(video_device->backlight,
1623 BACKLIGHT_UPDATE_HOTKEY);
1624 acpi_notifier_call_chain(device, event, 0);
1629 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
1630 brightness_switch_event(video_device, event);
1631 keycode = KEY_BRIGHTNESS_CYCLE;
1633 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
1634 brightness_switch_event(video_device, event);
1635 keycode = KEY_BRIGHTNESSUP;
1637 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
1638 brightness_switch_event(video_device, event);
1639 keycode = KEY_BRIGHTNESSDOWN;
1641 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
1642 brightness_switch_event(video_device, event);
1643 keycode = KEY_BRIGHTNESS_ZERO;
1645 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
1646 brightness_switch_event(video_device, event);
1647 keycode = KEY_DISPLAY_OFF;
1650 acpi_handle_debug(handle, "Unsupported event [0x%x]\n", event);
1655 may_report_brightness_keys = true;
1657 acpi_notifier_call_chain(device, event, 0);
1659 if (keycode && (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS)) {
1660 input_report_key(input, keycode, 1);
1662 input_report_key(input, keycode, 0);
1667 static int acpi_video_resume(struct notifier_block *nb,
1668 unsigned long val, void *ign)
1670 struct acpi_video_bus *video;
1671 struct acpi_video_device *video_device;
1675 case PM_POST_HIBERNATION:
1676 case PM_POST_SUSPEND:
1677 case PM_POST_RESTORE:
1678 video = container_of(nb, struct acpi_video_bus, pm_nb);
1680 dev_info(&video->device->dev, "Restoring backlight state\n");
1682 for (i = 0; i < video->attached_count; i++) {
1683 video_device = video->attached_array[i].bind_info;
1684 if (video_device && video_device->brightness)
1685 acpi_video_device_lcd_set_level(video_device,
1686 video_device->brightness->curr);
1695 acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1696 void **return_value)
1698 struct acpi_device *device = context;
1699 struct acpi_device *sibling;
1701 if (handle == device->handle)
1702 return AE_CTRL_TERMINATE;
1704 sibling = acpi_fetch_acpi_dev(handle);
1708 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1709 return AE_ALREADY_EXISTS;
1714 static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
1716 struct backlight_properties props;
1717 struct pci_dev *pdev;
1718 acpi_handle acpi_parent;
1719 struct device *parent = NULL;
1724 result = acpi_video_init_brightness(device);
1728 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
1733 acpi_get_parent(device->dev->handle, &acpi_parent);
1735 pdev = acpi_get_pci_dev(acpi_parent);
1737 parent = &pdev->dev;
1741 memset(&props, 0, sizeof(struct backlight_properties));
1742 props.type = BACKLIGHT_FIRMWARE;
1743 props.max_brightness =
1744 device->brightness->count - ACPI_VIDEO_FIRST_LEVEL - 1;
1745 device->backlight = backlight_device_register(name,
1748 &acpi_backlight_ops,
1751 if (IS_ERR(device->backlight)) {
1752 device->backlight = NULL;
1757 * Save current brightness level in case we have to restore it
1758 * before acpi_video_device_lcd_set_level() is called next time.
1760 device->backlight->props.brightness =
1761 acpi_video_get_brightness(device->backlight);
1763 device->cooling_dev = thermal_cooling_device_register("LCD",
1764 device->dev, &video_cooling_ops);
1765 if (IS_ERR(device->cooling_dev)) {
1767 * Set cooling_dev to NULL so we don't crash trying to free it.
1768 * Also, why the hell we are returning early and not attempt to
1769 * register video output if cooling device registration failed?
1772 device->cooling_dev = NULL;
1776 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1777 device->cooling_dev->id);
1778 result = sysfs_create_link(&device->dev->dev.kobj,
1779 &device->cooling_dev->device.kobj,
1782 pr_info("sysfs link creation failed\n");
1784 result = sysfs_create_link(&device->cooling_dev->device.kobj,
1785 &device->dev->dev.kobj, "device");
1787 pr_info("Reverse sysfs link creation failed\n");
1790 static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
1792 struct acpi_video_device *dev;
1793 union acpi_object *levels;
1795 mutex_lock(&video->device_list_lock);
1796 list_for_each_entry(dev, &video->video_device_list, entry) {
1797 if (!acpi_video_device_lcd_query_levels(dev->dev->handle, &levels))
1800 mutex_unlock(&video->device_list_lock);
1803 static bool acpi_video_should_register_backlight(struct acpi_video_device *dev)
1806 * Do not create backlight device for video output
1807 * device that is not in the enumerated list.
1809 if (!acpi_video_device_in_dod(dev)) {
1810 dev_dbg(&dev->dev->dev, "not in _DOD list, ignore\n");
1815 return dev->flags.lcd;
1819 static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
1821 struct acpi_video_device *dev;
1823 if (video->backlight_registered)
1826 if (acpi_video_get_backlight_type() != acpi_backlight_video)
1829 mutex_lock(&video->device_list_lock);
1830 list_for_each_entry(dev, &video->video_device_list, entry) {
1831 if (acpi_video_should_register_backlight(dev))
1832 acpi_video_dev_register_backlight(dev);
1834 mutex_unlock(&video->device_list_lock);
1836 video->backlight_registered = true;
1838 video->pm_nb.notifier_call = acpi_video_resume;
1839 video->pm_nb.priority = 0;
1840 return register_pm_notifier(&video->pm_nb);
1843 static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
1845 if (device->backlight) {
1846 backlight_device_unregister(device->backlight);
1847 device->backlight = NULL;
1849 if (device->brightness) {
1850 kfree(device->brightness->levels);
1851 kfree(device->brightness);
1852 device->brightness = NULL;
1854 if (device->cooling_dev) {
1855 sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
1856 sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
1857 thermal_cooling_device_unregister(device->cooling_dev);
1858 device->cooling_dev = NULL;
1862 static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
1864 struct acpi_video_device *dev;
1867 if (!video->backlight_registered)
1870 error = unregister_pm_notifier(&video->pm_nb);
1872 mutex_lock(&video->device_list_lock);
1873 list_for_each_entry(dev, &video->video_device_list, entry)
1874 acpi_video_dev_unregister_backlight(dev);
1875 mutex_unlock(&video->device_list_lock);
1877 video->backlight_registered = false;
1882 static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
1885 struct acpi_device *adev = device->dev;
1887 status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
1888 acpi_video_device_notify, device);
1889 if (ACPI_FAILURE(status))
1890 dev_err(&adev->dev, "Error installing notify handler\n");
1892 device->flags.notify = 1;
1895 static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video)
1897 struct input_dev *input;
1898 struct acpi_video_device *dev;
1901 video->input = input = input_allocate_device();
1907 error = acpi_video_bus_start_devices(video);
1909 goto err_free_input;
1911 snprintf(video->phys, sizeof(video->phys),
1912 "%s/video/input0", acpi_device_hid(video->device));
1914 input->name = acpi_device_name(video->device);
1915 input->phys = video->phys;
1916 input->id.bustype = BUS_HOST;
1917 input->id.product = 0x06;
1918 input->dev.parent = &video->device->dev;
1919 input->evbit[0] = BIT(EV_KEY);
1920 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1921 set_bit(KEY_VIDEO_NEXT, input->keybit);
1922 set_bit(KEY_VIDEO_PREV, input->keybit);
1923 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1924 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1925 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1926 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1927 set_bit(KEY_DISPLAY_OFF, input->keybit);
1929 error = input_register_device(input);
1933 mutex_lock(&video->device_list_lock);
1934 list_for_each_entry(dev, &video->video_device_list, entry)
1935 acpi_video_dev_add_notify_handler(dev);
1936 mutex_unlock(&video->device_list_lock);
1941 acpi_video_bus_stop_devices(video);
1943 input_free_device(input);
1944 video->input = NULL;
1949 static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
1951 if (dev->flags.notify) {
1952 acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
1953 acpi_video_device_notify);
1954 dev->flags.notify = 0;
1958 static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
1960 struct acpi_video_device *dev;
1962 mutex_lock(&video->device_list_lock);
1963 list_for_each_entry(dev, &video->video_device_list, entry)
1964 acpi_video_dev_remove_notify_handler(dev);
1965 mutex_unlock(&video->device_list_lock);
1967 acpi_video_bus_stop_devices(video);
1968 input_unregister_device(video->input);
1969 video->input = NULL;
1972 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1974 struct acpi_video_device *dev, *next;
1976 mutex_lock(&video->device_list_lock);
1977 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1978 list_del(&dev->entry);
1981 mutex_unlock(&video->device_list_lock);
1986 static int instance;
1988 static int acpi_video_bus_add(struct acpi_device *device)
1990 struct acpi_video_bus *video;
1994 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1995 acpi_dev_parent(device)->handle, 1,
1996 acpi_video_bus_match, NULL,
1998 if (status == AE_ALREADY_EXISTS) {
2000 "Duplicate ACPI video bus devices for the"
2001 " same VGA controller, please try module "
2002 "parameter \"video.allow_duplicates=1\""
2003 "if the current driver doesn't work.\n");
2004 if (!allow_duplicates)
2008 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
2012 /* a hack to fix the duplicate name "VID" problem on T61 */
2013 if (!strcmp(device->pnp.bus_id, "VID")) {
2015 device->pnp.bus_id[3] = '0' + instance;
2018 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2019 if (!strcmp(device->pnp.bus_id, "VGA")) {
2021 device->pnp.bus_id[3] = '0' + instance;
2025 video->device = device;
2026 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2027 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
2028 device->driver_data = video;
2030 acpi_video_bus_find_cap(video);
2031 error = acpi_video_bus_check(video);
2033 goto err_free_video;
2035 mutex_init(&video->device_list_lock);
2036 INIT_LIST_HEAD(&video->video_device_list);
2038 error = acpi_video_bus_get_devices(video, device);
2042 pr_info("%s [%s] (multi-head: %s rom: %s post: %s)\n",
2043 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2044 video->flags.multihead ? "yes" : "no",
2045 video->flags.rom ? "yes" : "no",
2046 video->flags.post ? "yes" : "no");
2047 mutex_lock(&video_list_lock);
2048 list_add_tail(&video->entry, &video_bus_head);
2049 mutex_unlock(&video_list_lock);
2052 * The userspace visible backlight_device gets registered separately
2053 * from acpi_video_register_backlight().
2055 acpi_video_run_bcl_for_osi(video);
2056 acpi_video_bus_add_notify_handler(video);
2061 acpi_video_bus_put_devices(video);
2062 kfree(video->attached_array);
2065 device->driver_data = NULL;
2070 static int acpi_video_bus_remove(struct acpi_device *device)
2072 struct acpi_video_bus *video = NULL;
2075 if (!device || !acpi_driver_data(device))
2078 video = acpi_driver_data(device);
2080 mutex_lock(&video_list_lock);
2081 list_del(&video->entry);
2082 mutex_unlock(&video_list_lock);
2084 acpi_video_bus_remove_notify_handler(video);
2085 acpi_video_bus_unregister_backlight(video);
2086 acpi_video_bus_put_devices(video);
2088 kfree(video->attached_array);
2094 static void acpi_video_bus_register_backlight_work(struct work_struct *ignored)
2096 acpi_video_register_backlight();
2099 static int __init is_i740(struct pci_dev *dev)
2101 if (dev->device == 0x00D1)
2103 if (dev->device == 0x7000)
2108 static int __init intel_opregion_present(void)
2111 struct pci_dev *dev = NULL;
2114 for_each_pci_dev(dev) {
2115 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2117 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2119 /* We don't want to poke around undefined i740 registers */
2122 pci_read_config_dword(dev, 0xfc, &address);
2130 /* Check if the chassis-type indicates there is no builtin LCD panel */
2131 static bool dmi_is_desktop(void)
2133 const char *chassis_type;
2136 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
2140 if (kstrtoul(chassis_type, 10, &type) != 0)
2144 case 0x03: /* Desktop */
2145 case 0x04: /* Low Profile Desktop */
2146 case 0x05: /* Pizza Box */
2147 case 0x06: /* Mini Tower */
2148 case 0x07: /* Tower */
2149 case 0x10: /* Lunch Box */
2150 case 0x11: /* Main Server Chassis */
2158 * We're seeing a lot of bogus backlight interfaces on newer machines
2159 * without a LCD such as desktops, servers and HDMI sticks. Checking the
2160 * lcd flag fixes this, enable this by default on any machines which are:
2161 * 1. Win8 ready (where we also prefer the native backlight driver, so
2162 * normally the acpi_video code should not register there anyways); *and*
2163 * 2.1 Report a desktop/server DMI chassis-type, or
2164 * 2.2 Are an ACPI-reduced-hardware platform (and thus won't use the EC for
2167 static bool should_check_lcd_flag(void)
2169 if (!acpi_osi_is_win8())
2172 if (dmi_is_desktop())
2175 if (acpi_reduced_hardware())
2181 int acpi_video_register(void)
2185 mutex_lock(®ister_count_mutex);
2186 if (register_count) {
2188 * if the function of acpi_video_register is already called,
2189 * don't register the acpi_video_bus again and return no error.
2195 only_lcd = should_check_lcd_flag();
2197 dmi_check_system(video_dmi_table);
2199 ret = acpi_bus_register_driver(&acpi_video_bus);
2204 * When the acpi_video_bus is loaded successfully, increase
2205 * the counter reference.
2210 * acpi_video_bus_add() skips registering the userspace visible
2211 * backlight_device. The intend is for this to be registered by the
2212 * drm/kms driver calling acpi_video_register_backlight() *after* it is
2213 * done setting up its own native backlight device. The delayed work
2214 * ensures that acpi_video_register_backlight() always gets called
2215 * eventually, in case there is no drm/kms driver or it is disabled.
2217 if (register_backlight_delay)
2218 schedule_delayed_work(&video_bus_register_backlight_work,
2219 register_backlight_delay * HZ);
2222 mutex_unlock(®ister_count_mutex);
2225 EXPORT_SYMBOL(acpi_video_register);
2227 void acpi_video_unregister(void)
2229 mutex_lock(®ister_count_mutex);
2230 if (register_count) {
2231 cancel_delayed_work_sync(&video_bus_register_backlight_work);
2232 acpi_bus_unregister_driver(&acpi_video_bus);
2234 may_report_brightness_keys = false;
2236 mutex_unlock(®ister_count_mutex);
2238 EXPORT_SYMBOL(acpi_video_unregister);
2240 void acpi_video_register_backlight(void)
2242 struct acpi_video_bus *video;
2244 mutex_lock(&video_list_lock);
2245 list_for_each_entry(video, &video_bus_head, entry)
2246 acpi_video_bus_register_backlight(video);
2247 mutex_unlock(&video_list_lock);
2249 EXPORT_SYMBOL(acpi_video_register_backlight);
2251 bool acpi_video_handles_brightness_key_presses(void)
2253 return may_report_brightness_keys &&
2254 (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS);
2256 EXPORT_SYMBOL(acpi_video_handles_brightness_key_presses);
2259 * This is kind of nasty. Hardware using Intel chipsets may require
2260 * the video opregion code to be run first in order to initialise
2261 * state before any ACPI video calls are made. To handle this we defer
2262 * registration of the video class until the opregion code has run.
2265 static int __init acpi_video_init(void)
2268 * Let the module load even if ACPI is disabled (e.g. due to
2269 * a broken BIOS) so that i915.ko can still be loaded on such
2270 * old systems without an AcpiOpRegion.
2272 * acpi_video_register() will report -ENODEV later as well due
2273 * to acpi_disabled when i915.ko tries to register itself afterwards.
2278 if (intel_opregion_present())
2281 return acpi_video_register();
2284 static void __exit acpi_video_exit(void)
2286 acpi_video_unregister();
2289 module_init(acpi_video_init);
2290 module_exit(acpi_video_exit);