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