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