xref: /linux/drivers/acpi/acpi_video.c (revision 5315c0ddbefe415abba4c89568ecfd27f79c5aba)
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/platform_device.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 ACPI_VIDEO_BUS_NAME		"Video Bus"
34 #define ACPI_VIDEO_DEVICE_NAME		"Video Device"
35 
36 #define MAX_NAME_LEN	20
37 
38 MODULE_AUTHOR("Bruno Ducrot");
39 MODULE_DESCRIPTION("ACPI Video Driver");
40 MODULE_LICENSE("GPL");
41 
42 static bool brightness_switch_enabled = true;
43 module_param(brightness_switch_enabled, bool, 0644);
44 
45 /*
46  * By default, we don't allow duplicate ACPI video bus devices
47  * under the same VGA controller
48  */
49 static bool allow_duplicates;
50 module_param(allow_duplicates, bool, 0644);
51 
52 #define REPORT_OUTPUT_KEY_EVENTS		0x01
53 #define REPORT_BRIGHTNESS_KEY_EVENTS		0x02
54 static int report_key_events = -1;
55 module_param(report_key_events, int, 0644);
56 MODULE_PARM_DESC(report_key_events,
57 	"0: none, 1: output changes, 2: brightness changes, 3: all");
58 
59 static int hw_changes_brightness = -1;
60 module_param(hw_changes_brightness, int, 0644);
61 MODULE_PARM_DESC(hw_changes_brightness,
62 	"Set this to 1 on buggy hw which changes the brightness itself when "
63 	"a hotkey is pressed: -1: auto, 0: normal 1: hw-changes-brightness");
64 
65 /*
66  * Whether the struct acpi_video_device_attrib::device_id_scheme bit should be
67  * assumed even if not actually set.
68  */
69 static bool device_id_scheme = false;
70 module_param(device_id_scheme, bool, 0444);
71 
72 static int only_lcd;
73 module_param(only_lcd, int, 0444);
74 
75 static bool may_report_brightness_keys;
76 static int register_count;
77 static DEFINE_MUTEX(register_count_mutex);
78 static DEFINE_MUTEX(video_list_lock);
79 static LIST_HEAD(video_bus_head);
80 static int acpi_video_bus_probe(struct platform_device *pdev);
81 static void acpi_video_bus_remove(struct platform_device *pdev);
82 static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data);
83 
84 /*
85  * Indices in the _BCL method response: the first two items are special,
86  * the rest are all supported levels.
87  *
88  * See page 575 of the ACPI spec 3.0
89  */
90 enum acpi_video_level_idx {
91 	ACPI_VIDEO_AC_LEVEL,		/* level when machine has full power */
92 	ACPI_VIDEO_BATTERY_LEVEL,	/* level when machine is on batteries */
93 	ACPI_VIDEO_FIRST_LEVEL,		/* actual supported levels begin here */
94 };
95 
96 static const struct acpi_device_id video_device_ids[] = {
97 	{ACPI_VIDEO_HID, 0},
98 	{"", 0},
99 };
100 MODULE_DEVICE_TABLE(acpi, video_device_ids);
101 
102 static struct platform_driver acpi_video_bus = {
103 	.probe = acpi_video_bus_probe,
104 	.remove = acpi_video_bus_remove,
105 	.driver = {
106 		.name = "acpi-video",
107 		.acpi_match_table = video_device_ids,
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 	/*
653 	 * Some buggy implementations incorrectly return the EDID buffer in an ACPI package.
654 	 * In this case, extract the buffer from the package.
655 	 */
656 	if (obj && obj->type == ACPI_TYPE_PACKAGE && obj->package.count == 1)
657 		obj = &obj->package.elements[0];
658 
659 	if (obj && obj->type == ACPI_TYPE_BUFFER) {
660 		*edid = kmemdup(obj->buffer.pointer, obj->buffer.length, GFP_KERNEL);
661 		ret = *edid ? obj->buffer.length : -ENOMEM;
662 	} else {
663 		acpi_handle_debug(device->dev->handle,
664 				 "Invalid _DDC data for length %d\n", length);
665 		ret = -EFAULT;
666 	}
667 
668 	kfree(buffer.pointer);
669 	return ret;
670 }
671 
672 /* bus */
673 
674 /*
675  *  Arg:
676  *	video		: video bus device pointer
677  *	bios_flag	:
678  *		0.	The system BIOS should NOT automatically switch(toggle)
679  *			the active display output.
680  *		1.	The system BIOS should automatically switch (toggle) the
681  *			active display output. No switch event.
682  *		2.	The _DGS value should be locked.
683  *		3.	The system BIOS should not automatically switch (toggle) the
684  *			active display output, but instead generate the display switch
685  *			event notify code.
686  *	lcd_flag	:
687  *		0.	The system BIOS should automatically control the brightness level
688  *			of the LCD when:
689  *			- the power changes from AC to DC (ACPI appendix B)
690  *			- a brightness hotkey gets pressed (implied by Win7/8 backlight docs)
691  *		1.	The system BIOS should NOT automatically control the brightness
692  *			level of the LCD when:
693  *			- the power changes from AC to DC (ACPI appendix B)
694  *			- a brightness hotkey gets pressed (implied by Win7/8 backlight docs)
695  *  Return Value:
696  *		-EINVAL	wrong arg.
697  */
698 
699 static int
700 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
701 {
702 	acpi_status status;
703 
704 	if (!video->cap._DOS)
705 		return 0;
706 
707 	if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
708 		return -EINVAL;
709 	video->dos_setting = (lcd_flag << 2) | bios_flag;
710 	status = acpi_execute_simple_method(video->device->handle, "_DOS",
711 					    (lcd_flag << 2) | bios_flag);
712 	if (ACPI_FAILURE(status))
713 		return -EIO;
714 
715 	return 0;
716 }
717 
718 /*
719  * Simple comparison function used to sort backlight levels.
720  */
721 
722 static int
723 acpi_video_cmp_level(const void *a, const void *b)
724 {
725 	return *(int *)a - *(int *)b;
726 }
727 
728 /*
729  * Decides if _BQC/_BCQ for this system is usable
730  *
731  * We do this by changing the level first and then read out the current
732  * brightness level, if the value does not match, find out if it is using
733  * index. If not, clear the _BQC/_BCQ capability.
734  */
735 static int acpi_video_bqc_quirk(struct acpi_video_device *device,
736 				int max_level, int current_level)
737 {
738 	struct acpi_video_device_brightness *br = device->brightness;
739 	int result;
740 	unsigned long long level;
741 	int test_level;
742 
743 	/* don't mess with existing known broken systems */
744 	if (bqc_offset_aml_bug_workaround)
745 		return 0;
746 
747 	/*
748 	 * Some systems always report current brightness level as maximum
749 	 * through _BQC, we need to test another value for them. However,
750 	 * there is a subtlety:
751 	 *
752 	 * If the _BCL package ordering is descending, the first level
753 	 * (br->levels[2]) is likely to be 0, and if the number of levels
754 	 * matches the number of steps, we might confuse a returned level to
755 	 * mean the index.
756 	 *
757 	 * For example:
758 	 *
759 	 *     current_level = max_level = 100
760 	 *     test_level = 0
761 	 *     returned level = 100
762 	 *
763 	 * In this case 100 means the level, not the index, and _BCM failed.
764 	 * Still, if the _BCL package ordering is descending, the index of
765 	 * level 0 is also 100, so we assume _BQC is indexed, when it's not.
766 	 *
767 	 * This causes all _BQC calls to return bogus values causing weird
768 	 * behavior from the user's perspective.  For example:
769 	 *
770 	 * xbacklight -set 10; xbacklight -set 20;
771 	 *
772 	 * would flash to 90% and then slowly down to the desired level (20).
773 	 *
774 	 * The solution is simple; test anything other than the first level
775 	 * (e.g. 1).
776 	 */
777 	test_level = current_level == max_level
778 		? br->levels[ACPI_VIDEO_FIRST_LEVEL + 1]
779 		: max_level;
780 
781 	result = acpi_video_device_lcd_set_level(device, test_level);
782 	if (result)
783 		return result;
784 
785 	result = acpi_video_device_lcd_get_level_current(device, &level, true);
786 	if (result)
787 		return result;
788 
789 	if (level != test_level) {
790 		/* buggy _BQC found, need to find out if it uses index */
791 		if (level < br->count) {
792 			if (br->flags._BCL_reversed)
793 				level = br->count - ACPI_VIDEO_FIRST_LEVEL - 1 - level;
794 			if (br->levels[level + ACPI_VIDEO_FIRST_LEVEL] == test_level)
795 				br->flags._BQC_use_index = 1;
796 		}
797 
798 		if (!br->flags._BQC_use_index)
799 			device->cap._BQC = device->cap._BCQ = 0;
800 	}
801 
802 	return 0;
803 }
804 
805 int acpi_video_get_levels(struct acpi_device *device,
806 			  struct acpi_video_device_brightness **dev_br,
807 			  int *pmax_level)
808 {
809 	union acpi_object *obj = NULL;
810 	int i, max_level = 0, count = 0, level_ac_battery = 0;
811 	union acpi_object *o;
812 	struct acpi_video_device_brightness *br = NULL;
813 	int result = 0;
814 	u32 value;
815 
816 	if (ACPI_FAILURE(acpi_video_device_lcd_query_levels(device->handle, &obj))) {
817 		acpi_handle_debug(device->handle,
818 				  "Could not query available LCD brightness level\n");
819 		result = -ENODEV;
820 		goto out;
821 	}
822 
823 	if (obj->package.count < ACPI_VIDEO_FIRST_LEVEL) {
824 		result = -EINVAL;
825 		goto out;
826 	}
827 
828 	br = kzalloc(sizeof(*br), GFP_KERNEL);
829 	if (!br) {
830 		result = -ENOMEM;
831 		goto out;
832 	}
833 
834 	/*
835 	 * Note that we have to reserve 2 extra items (ACPI_VIDEO_FIRST_LEVEL),
836 	 * in order to account for buggy BIOS which don't export the first two
837 	 * special levels (see below)
838 	 */
839 	br->levels = kmalloc_array(obj->package.count + ACPI_VIDEO_FIRST_LEVEL,
840 				   sizeof(*br->levels),
841 				   GFP_KERNEL);
842 	if (!br->levels) {
843 		result = -ENOMEM;
844 		goto out_free;
845 	}
846 
847 	for (i = 0; i < obj->package.count; i++) {
848 		o = (union acpi_object *)&obj->package.elements[i];
849 		if (o->type != ACPI_TYPE_INTEGER) {
850 			acpi_handle_info(device->handle, "Invalid data\n");
851 			continue;
852 		}
853 		value = (u32) o->integer.value;
854 		/* Skip duplicate entries */
855 		if (count > ACPI_VIDEO_FIRST_LEVEL
856 		    && br->levels[count - 1] == value)
857 			continue;
858 
859 		br->levels[count] = value;
860 
861 		if (br->levels[count] > max_level)
862 			max_level = br->levels[count];
863 		count++;
864 	}
865 
866 	/*
867 	 * some buggy BIOS don't export the levels
868 	 * when machine is on AC/Battery in _BCL package.
869 	 * In this case, the first two elements in _BCL packages
870 	 * are also supported brightness levels that OS should take care of.
871 	 */
872 	for (i = ACPI_VIDEO_FIRST_LEVEL; i < count; i++) {
873 		if (br->levels[i] == br->levels[ACPI_VIDEO_AC_LEVEL])
874 			level_ac_battery++;
875 		if (br->levels[i] == br->levels[ACPI_VIDEO_BATTERY_LEVEL])
876 			level_ac_battery++;
877 	}
878 
879 	if (level_ac_battery < ACPI_VIDEO_FIRST_LEVEL) {
880 		level_ac_battery = ACPI_VIDEO_FIRST_LEVEL - level_ac_battery;
881 		br->flags._BCL_no_ac_battery_levels = 1;
882 		for (i = (count - 1 + level_ac_battery);
883 		     i >= ACPI_VIDEO_FIRST_LEVEL; i--)
884 			br->levels[i] = br->levels[i - level_ac_battery];
885 		count += level_ac_battery;
886 	} else if (level_ac_battery > ACPI_VIDEO_FIRST_LEVEL)
887 		acpi_handle_info(device->handle,
888 				 "Too many duplicates in _BCL package");
889 
890 	/* Check if the _BCL package is in a reversed order */
891 	if (max_level == br->levels[ACPI_VIDEO_FIRST_LEVEL]) {
892 		br->flags._BCL_reversed = 1;
893 		sort(&br->levels[ACPI_VIDEO_FIRST_LEVEL],
894 		     count - ACPI_VIDEO_FIRST_LEVEL,
895 		     sizeof(br->levels[ACPI_VIDEO_FIRST_LEVEL]),
896 		     acpi_video_cmp_level, NULL);
897 	} else if (max_level != br->levels[count - 1])
898 		acpi_handle_info(device->handle,
899 				 "Found unordered _BCL package");
900 
901 	br->count = count;
902 	*dev_br = br;
903 	if (pmax_level)
904 		*pmax_level = max_level;
905 
906 out:
907 	kfree(obj);
908 	return result;
909 out_free:
910 	kfree(br);
911 	goto out;
912 }
913 EXPORT_SYMBOL(acpi_video_get_levels);
914 
915 /*
916  *  Arg:
917  *	device	: video output device (LCD, CRT, ..)
918  *
919  *  Return Value:
920  *	Maximum brightness level
921  *
922  *  Allocate and initialize device->brightness.
923  */
924 
925 static int
926 acpi_video_init_brightness(struct acpi_video_device *device)
927 {
928 	int i, max_level = 0;
929 	unsigned long long level, level_old;
930 	struct acpi_video_device_brightness *br = NULL;
931 	int result;
932 
933 	result = acpi_video_get_levels(device->dev, &br, &max_level);
934 	if (result)
935 		return result;
936 	device->brightness = br;
937 
938 	/* _BQC uses INDEX while _BCL uses VALUE in some laptops */
939 	br->curr = level = max_level;
940 
941 	if (!device->cap._BQC)
942 		goto set_level;
943 
944 	result = acpi_video_device_lcd_get_level_current(device,
945 							 &level_old, true);
946 	if (result)
947 		goto out_free_levels;
948 
949 	result = acpi_video_bqc_quirk(device, max_level, level_old);
950 	if (result)
951 		goto out_free_levels;
952 	/*
953 	 * cap._BQC may get cleared due to _BQC is found to be broken
954 	 * in acpi_video_bqc_quirk, so check again here.
955 	 */
956 	if (!device->cap._BQC)
957 		goto set_level;
958 
959 	level = acpi_video_bqc_value_to_level(device, level_old);
960 	/*
961 	 * On some buggy laptops, _BQC returns an uninitialized
962 	 * value when invoked for the first time, i.e.
963 	 * level_old is invalid (no matter whether it's a level
964 	 * or an index). Set the backlight to max_level in this case.
965 	 */
966 	for (i = ACPI_VIDEO_FIRST_LEVEL; i < br->count; i++)
967 		if (level == br->levels[i])
968 			break;
969 	if (i == br->count || !level)
970 		level = max_level;
971 
972 set_level:
973 	result = acpi_video_device_lcd_set_level(device, level);
974 	if (result)
975 		goto out_free_levels;
976 
977 	acpi_handle_debug(device->dev->handle, "found %d brightness levels\n",
978 			  br->count - ACPI_VIDEO_FIRST_LEVEL);
979 
980 	return 0;
981 
982 out_free_levels:
983 	kfree(br->levels);
984 	kfree(br);
985 	device->brightness = NULL;
986 	return result;
987 }
988 
989 /*
990  *  Arg:
991  *	device	: video output device (LCD, CRT, ..)
992  *
993  *  Return Value:
994  *	None
995  *
996  *  Find out all required AML methods defined under the output
997  *  device.
998  */
999 
1000 static void acpi_video_device_find_cap(struct acpi_video_device *device)
1001 {
1002 	if (acpi_has_method(device->dev->handle, "_ADR"))
1003 		device->cap._ADR = 1;
1004 	if (acpi_has_method(device->dev->handle, "_BCL"))
1005 		device->cap._BCL = 1;
1006 	if (acpi_has_method(device->dev->handle, "_BCM"))
1007 		device->cap._BCM = 1;
1008 	if (acpi_has_method(device->dev->handle, "_BQC")) {
1009 		device->cap._BQC = 1;
1010 	} else if (acpi_has_method(device->dev->handle, "_BCQ")) {
1011 		acpi_handle_info(device->dev->handle,
1012 				 "_BCQ is used instead of _BQC\n");
1013 		device->cap._BCQ = 1;
1014 	}
1015 
1016 	if (acpi_has_method(device->dev->handle, "_DDC"))
1017 		device->cap._DDC = 1;
1018 }
1019 
1020 /*
1021  *  Arg:
1022  *	device	: video output device (VGA)
1023  *
1024  *  Return Value:
1025  *	None
1026  *
1027  *  Find out all required AML methods defined under the video bus device.
1028  */
1029 
1030 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
1031 {
1032 	if (acpi_has_method(video->device->handle, "_DOS"))
1033 		video->cap._DOS = 1;
1034 	if (acpi_has_method(video->device->handle, "_DOD"))
1035 		video->cap._DOD = 1;
1036 	if (acpi_has_method(video->device->handle, "_ROM"))
1037 		video->cap._ROM = 1;
1038 	if (acpi_has_method(video->device->handle, "_GPD"))
1039 		video->cap._GPD = 1;
1040 	if (acpi_has_method(video->device->handle, "_SPD"))
1041 		video->cap._SPD = 1;
1042 	if (acpi_has_method(video->device->handle, "_VPO"))
1043 		video->cap._VPO = 1;
1044 }
1045 
1046 /*
1047  * Check whether the video bus device has required AML method to
1048  * support the desired features
1049  */
1050 
1051 static int acpi_video_bus_check(struct acpi_video_bus *video)
1052 {
1053 	acpi_status status = -ENOENT;
1054 	struct pci_dev *dev;
1055 
1056 	if (!video)
1057 		return -EINVAL;
1058 
1059 	dev = acpi_get_pci_dev(video->device->handle);
1060 	if (!dev)
1061 		return -ENODEV;
1062 	pci_dev_put(dev);
1063 
1064 	/*
1065 	 * Since there is no HID, CID and so on for VGA driver, we have
1066 	 * to check well known required nodes.
1067 	 */
1068 
1069 	/* Does this device support video switching? */
1070 	if (video->cap._DOS || video->cap._DOD) {
1071 		if (!video->cap._DOS) {
1072 			pr_info(FW_BUG "ACPI(%s) defines _DOD but not _DOS\n",
1073 				acpi_device_bid(video->device));
1074 		}
1075 		video->flags.multihead = 1;
1076 		status = 0;
1077 	}
1078 
1079 	/* Does this device support retrieving a video ROM? */
1080 	if (video->cap._ROM) {
1081 		video->flags.rom = 1;
1082 		status = 0;
1083 	}
1084 
1085 	/* Does this device support configuring which video device to POST? */
1086 	if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
1087 		video->flags.post = 1;
1088 		status = 0;
1089 	}
1090 
1091 	return status;
1092 }
1093 
1094 /*
1095  * --------------------------------------------------------------------------
1096  *                               Driver Interface
1097  * --------------------------------------------------------------------------
1098  */
1099 
1100 /* device interface */
1101 static struct acpi_video_device_attrib *
1102 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1103 {
1104 	struct acpi_video_enumerated_device *ids;
1105 	int i;
1106 
1107 	for (i = 0; i < video->attached_count; i++) {
1108 		ids = &video->attached_array[i];
1109 		if ((ids->value.int_val & 0xffff) == device_id)
1110 			return &ids->value.attrib;
1111 	}
1112 
1113 	return NULL;
1114 }
1115 
1116 static int
1117 acpi_video_get_device_type(struct acpi_video_bus *video,
1118 			   unsigned long device_id)
1119 {
1120 	struct acpi_video_enumerated_device *ids;
1121 	int i;
1122 
1123 	for (i = 0; i < video->attached_count; i++) {
1124 		ids = &video->attached_array[i];
1125 		if ((ids->value.int_val & 0xffff) == device_id)
1126 			return ids->value.int_val;
1127 	}
1128 
1129 	return 0;
1130 }
1131 
1132 static int acpi_video_bus_get_one_device(struct acpi_device *device, void *arg)
1133 {
1134 	struct acpi_video_bus *video = arg;
1135 	struct acpi_video_device_attrib *attribute;
1136 	struct acpi_video_device *data;
1137 	int device_type;
1138 	u64 device_id;
1139 
1140 	/* Skip devices without _ADR instead of failing. */
1141 	if (acpi_get_local_u64_address(device->handle, &device_id))
1142 		goto exit;
1143 
1144 	data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1145 	if (!data) {
1146 		dev_dbg(&device->dev, "Cannot attach\n");
1147 		return -ENOMEM;
1148 	}
1149 
1150 	strscpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1151 	strscpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1152 
1153 	data->device_id = device_id;
1154 	data->video = video;
1155 	data->dev = device;
1156 	INIT_DELAYED_WORK(&data->switch_brightness_work,
1157 			  acpi_video_switch_brightness);
1158 
1159 	attribute = acpi_video_get_device_attr(video, device_id);
1160 
1161 	if (attribute && (attribute->device_id_scheme || device_id_scheme)) {
1162 		switch (attribute->display_type) {
1163 		case ACPI_VIDEO_DISPLAY_CRT:
1164 			data->flags.crt = 1;
1165 			break;
1166 		case ACPI_VIDEO_DISPLAY_TV:
1167 			data->flags.tvout = 1;
1168 			break;
1169 		case ACPI_VIDEO_DISPLAY_DVI:
1170 			data->flags.dvi = 1;
1171 			break;
1172 		case ACPI_VIDEO_DISPLAY_LCD:
1173 			data->flags.lcd = 1;
1174 			break;
1175 		default:
1176 			data->flags.unknown = 1;
1177 			break;
1178 		}
1179 		if (attribute->bios_can_detect)
1180 			data->flags.bios = 1;
1181 	} else {
1182 		/* Check for legacy IDs */
1183 		device_type = acpi_video_get_device_type(video, device_id);
1184 		/* Ignore bits 16 and 18-20 */
1185 		switch (device_type & 0xffe2ffff) {
1186 		case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1187 			data->flags.crt = 1;
1188 			break;
1189 		case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1190 			data->flags.lcd = 1;
1191 			break;
1192 		case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1193 			data->flags.tvout = 1;
1194 			break;
1195 		default:
1196 			data->flags.unknown = 1;
1197 		}
1198 	}
1199 
1200 	acpi_video_device_bind(video, data);
1201 	acpi_video_device_find_cap(data);
1202 
1203 	if (data->cap._BCM && data->cap._BCL)
1204 		may_report_brightness_keys = true;
1205 
1206 	mutex_lock(&video->device_list_lock);
1207 	list_add_tail(&data->entry, &video->video_device_list);
1208 	mutex_unlock(&video->device_list_lock);
1209 
1210 exit:
1211 	video->child_count++;
1212 	return 0;
1213 }
1214 
1215 /*
1216  *  Arg:
1217  *	video	: video bus device
1218  *
1219  *  Return:
1220  *	none
1221  *
1222  *  Enumerate the video device list of the video bus,
1223  *  bind the ids with the corresponding video devices
1224  *  under the video bus.
1225  */
1226 
1227 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1228 {
1229 	struct acpi_video_device *dev;
1230 
1231 	mutex_lock(&video->device_list_lock);
1232 
1233 	list_for_each_entry(dev, &video->video_device_list, entry)
1234 		acpi_video_device_bind(video, dev);
1235 
1236 	mutex_unlock(&video->device_list_lock);
1237 }
1238 
1239 /*
1240  *  Arg:
1241  *	video	: video bus device
1242  *	device	: video output device under the video
1243  *		bus
1244  *
1245  *  Return:
1246  *	none
1247  *
1248  *  Bind the ids with the corresponding video devices
1249  *  under the video bus.
1250  */
1251 
1252 static void
1253 acpi_video_device_bind(struct acpi_video_bus *video,
1254 		       struct acpi_video_device *device)
1255 {
1256 	struct acpi_video_enumerated_device *ids;
1257 	int i;
1258 
1259 	for (i = 0; i < video->attached_count; i++) {
1260 		ids = &video->attached_array[i];
1261 		if (device->device_id == (ids->value.int_val & 0xffff)) {
1262 			ids->bind_info = device;
1263 			acpi_handle_debug(video->device->handle, "%s: %d\n",
1264 					  __func__, i);
1265 		}
1266 	}
1267 }
1268 
1269 static bool acpi_video_device_in_dod(struct acpi_video_device *device)
1270 {
1271 	struct acpi_video_bus *video = device->video;
1272 	int i;
1273 
1274 	/*
1275 	 * If we have a broken _DOD or we have more than 8 output devices
1276 	 * under the graphics controller node that we can't proper deal with
1277 	 * in the operation region code currently, no need to test.
1278 	 */
1279 	if (!video->attached_count || video->child_count > 8)
1280 		return true;
1281 
1282 	for (i = 0; i < video->attached_count; i++) {
1283 		if ((video->attached_array[i].value.int_val & 0xfff) ==
1284 		    (device->device_id & 0xfff))
1285 			return true;
1286 	}
1287 
1288 	return false;
1289 }
1290 
1291 /*
1292  *  Arg:
1293  *	video	: video bus device
1294  *
1295  *  Return:
1296  *	< 0	: error
1297  *
1298  *  Call _DOD to enumerate all devices attached to display adapter
1299  *
1300  */
1301 
1302 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1303 {
1304 	int status;
1305 	int count;
1306 	int i;
1307 	struct acpi_video_enumerated_device *active_list;
1308 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1309 	union acpi_object *dod = NULL;
1310 	union acpi_object *obj;
1311 
1312 	if (!video->cap._DOD)
1313 		return AE_NOT_EXIST;
1314 
1315 	status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1316 	if (ACPI_FAILURE(status)) {
1317 		acpi_handle_info(video->device->handle,
1318 				 "_DOD evaluation failed: %s\n",
1319 				 acpi_format_exception(status));
1320 		return status;
1321 	}
1322 
1323 	dod = buffer.pointer;
1324 	if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1325 		acpi_handle_info(video->device->handle, "Invalid _DOD data\n");
1326 		status = -EFAULT;
1327 		goto out;
1328 	}
1329 
1330 	acpi_handle_debug(video->device->handle, "Found %d video heads in _DOD\n",
1331 			  dod->package.count);
1332 
1333 	active_list = kcalloc(1 + dod->package.count,
1334 			      sizeof(struct acpi_video_enumerated_device),
1335 			      GFP_KERNEL);
1336 	if (!active_list) {
1337 		status = -ENOMEM;
1338 		goto out;
1339 	}
1340 
1341 	count = 0;
1342 	for (i = 0; i < dod->package.count; i++) {
1343 		obj = &dod->package.elements[i];
1344 
1345 		if (obj->type != ACPI_TYPE_INTEGER) {
1346 			acpi_handle_info(video->device->handle,
1347 					 "Invalid _DOD data in element %d\n", i);
1348 			continue;
1349 		}
1350 
1351 		active_list[count].value.int_val = obj->integer.value;
1352 		active_list[count].bind_info = NULL;
1353 
1354 		acpi_handle_debug(video->device->handle,
1355 				  "_DOD element[%d] = %d\n", i,
1356 				  (int)obj->integer.value);
1357 
1358 		count++;
1359 	}
1360 
1361 	kfree(video->attached_array);
1362 
1363 	video->attached_array = active_list;
1364 	video->attached_count = count;
1365 
1366 out:
1367 	kfree(buffer.pointer);
1368 	return status;
1369 }
1370 
1371 static int
1372 acpi_video_get_next_level(struct acpi_video_device *device,
1373 			  u32 level_current, u32 event)
1374 {
1375 	int min, max, min_above, max_below, i, l, delta = 255;
1376 	max = max_below = 0;
1377 	min = min_above = 255;
1378 	/* Find closest level to level_current */
1379 	for (i = ACPI_VIDEO_FIRST_LEVEL; i < device->brightness->count; i++) {
1380 		l = device->brightness->levels[i];
1381 		if (abs(l - level_current) < abs(delta)) {
1382 			delta = l - level_current;
1383 			if (!delta)
1384 				break;
1385 		}
1386 	}
1387 	/* Adjust level_current to closest available level */
1388 	level_current += delta;
1389 	for (i = ACPI_VIDEO_FIRST_LEVEL; i < device->brightness->count; i++) {
1390 		l = device->brightness->levels[i];
1391 		if (l < min)
1392 			min = l;
1393 		if (l > max)
1394 			max = l;
1395 		if (l < min_above && l > level_current)
1396 			min_above = l;
1397 		if (l > max_below && l < level_current)
1398 			max_below = l;
1399 	}
1400 
1401 	switch (event) {
1402 	case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1403 		return (level_current < max) ? min_above : min;
1404 	case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1405 		return (level_current < max) ? min_above : max;
1406 	case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1407 		return (level_current > min) ? max_below : min;
1408 	case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1409 	case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1410 		return 0;
1411 	default:
1412 		return level_current;
1413 	}
1414 }
1415 
1416 static void
1417 acpi_video_switch_brightness(struct work_struct *work)
1418 {
1419 	struct acpi_video_device *device = container_of(to_delayed_work(work),
1420 			     struct acpi_video_device, switch_brightness_work);
1421 	unsigned long long level_current, level_next;
1422 	int event = device->switch_brightness_event;
1423 	int result = -EINVAL;
1424 
1425 	/* no warning message if acpi_backlight=vendor or a quirk is used */
1426 	if (!device->backlight)
1427 		return;
1428 
1429 	if (!device->brightness)
1430 		goto out;
1431 
1432 	result = acpi_video_device_lcd_get_level_current(device,
1433 							 &level_current,
1434 							 false);
1435 	if (result)
1436 		goto out;
1437 
1438 	level_next = acpi_video_get_next_level(device, level_current, event);
1439 
1440 	result = acpi_video_device_lcd_set_level(device, level_next);
1441 
1442 	if (!result)
1443 		backlight_force_update(device->backlight,
1444 				       BACKLIGHT_UPDATE_HOTKEY);
1445 
1446 out:
1447 	if (result)
1448 		acpi_handle_info(device->dev->handle,
1449 				 "Failed to switch brightness\n");
1450 }
1451 
1452 int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1453 			void **edid)
1454 {
1455 	struct acpi_video_bus *video;
1456 	struct acpi_video_device *video_device;
1457 	int i, length, ret;
1458 
1459 	if (!device || !acpi_driver_data(device))
1460 		return -EINVAL;
1461 
1462 	video = acpi_driver_data(device);
1463 
1464 	for (i = 0; i < video->attached_count; i++) {
1465 		video_device = video->attached_array[i].bind_info;
1466 
1467 		if (!video_device)
1468 			continue;
1469 
1470 		if (!video_device->cap._DDC)
1471 			continue;
1472 
1473 		if (type) {
1474 			switch (type) {
1475 			case ACPI_VIDEO_DISPLAY_CRT:
1476 				if (!video_device->flags.crt)
1477 					continue;
1478 				break;
1479 			case ACPI_VIDEO_DISPLAY_TV:
1480 				if (!video_device->flags.tvout)
1481 					continue;
1482 				break;
1483 			case ACPI_VIDEO_DISPLAY_DVI:
1484 				if (!video_device->flags.dvi)
1485 					continue;
1486 				break;
1487 			case ACPI_VIDEO_DISPLAY_LCD:
1488 				if (!video_device->flags.lcd)
1489 					continue;
1490 				break;
1491 			}
1492 		} else if (video_device->device_id != device_id) {
1493 			continue;
1494 		}
1495 
1496 		for (length = 512; length > 0; length -= 128) {
1497 			ret = acpi_video_device_EDID(video_device, edid, length);
1498 			if (ret > 0)
1499 				return ret;
1500 		}
1501 	}
1502 
1503 	return -ENODEV;
1504 }
1505 EXPORT_SYMBOL(acpi_video_get_edid);
1506 
1507 static int
1508 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1509 			   struct acpi_device *device)
1510 {
1511 	/*
1512 	 * There are systems where video module known to work fine regardless
1513 	 * of broken _DOD and ignoring returned value here doesn't cause
1514 	 * any issues later.
1515 	 */
1516 	acpi_video_device_enumerate(video);
1517 
1518 	return acpi_dev_for_each_child(device, acpi_video_bus_get_one_device, video);
1519 }
1520 
1521 /* acpi_video interface */
1522 
1523 /*
1524  * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1525  * perform any automatic brightness change on receiving a notification.
1526  */
1527 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1528 {
1529 	return acpi_video_bus_DOS(video, 0,
1530 				  acpi_osi_is_win8() ? 1 : 0);
1531 }
1532 
1533 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1534 {
1535 	return acpi_video_bus_DOS(video, 0,
1536 				  acpi_osi_is_win8() ? 0 : 1);
1537 }
1538 
1539 static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
1540 {
1541 	struct acpi_video_bus *video = data;
1542 	struct acpi_device *device = video->device;
1543 	struct input_dev *input;
1544 	int keycode = 0;
1545 
1546 	input = video->input;
1547 
1548 	switch (event) {
1549 	case ACPI_VIDEO_NOTIFY_SWITCH:	/* User requested a switch,
1550 					 * most likely via hotkey. */
1551 		keycode = KEY_SWITCHVIDEOMODE;
1552 		break;
1553 
1554 	case ACPI_VIDEO_NOTIFY_PROBE:	/* User plugged in or removed a video
1555 					 * connector. */
1556 		acpi_video_device_enumerate(video);
1557 		acpi_video_device_rebind(video);
1558 		keycode = KEY_SWITCHVIDEOMODE;
1559 		break;
1560 
1561 	case ACPI_VIDEO_NOTIFY_CYCLE:	/* Cycle Display output hotkey pressed. */
1562 		keycode = KEY_SWITCHVIDEOMODE;
1563 		break;
1564 	case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:	/* Next Display output hotkey pressed. */
1565 		keycode = KEY_VIDEO_NEXT;
1566 		break;
1567 	case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:	/* previous Display output hotkey pressed. */
1568 		keycode = KEY_VIDEO_PREV;
1569 		break;
1570 
1571 	default:
1572 		acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n",
1573 				  event);
1574 		break;
1575 	}
1576 
1577 	if (acpi_notifier_call_chain(device, event, 0))
1578 		/* Something vetoed the keypress. */
1579 		keycode = 0;
1580 
1581 	if (keycode && (report_key_events & REPORT_OUTPUT_KEY_EVENTS)) {
1582 		input_report_key(input, keycode, 1);
1583 		input_sync(input);
1584 		input_report_key(input, keycode, 0);
1585 		input_sync(input);
1586 	}
1587 }
1588 
1589 static void brightness_switch_event(struct acpi_video_device *video_device,
1590 				    u32 event)
1591 {
1592 	if (!brightness_switch_enabled)
1593 		return;
1594 
1595 	video_device->switch_brightness_event = event;
1596 	schedule_delayed_work(&video_device->switch_brightness_work, HZ / 10);
1597 }
1598 
1599 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1600 {
1601 	struct acpi_video_device *video_device = data;
1602 	struct acpi_device *device = NULL;
1603 	struct acpi_video_bus *bus;
1604 	struct input_dev *input;
1605 	int keycode = 0;
1606 
1607 	if (!video_device)
1608 		return;
1609 
1610 	device = video_device->dev;
1611 	bus = video_device->video;
1612 	input = bus->input;
1613 
1614 	if (hw_changes_brightness > 0) {
1615 		if (video_device->backlight)
1616 			backlight_force_update(video_device->backlight,
1617 					       BACKLIGHT_UPDATE_HOTKEY);
1618 		acpi_notifier_call_chain(device, event, 0);
1619 		return;
1620 	}
1621 
1622 	switch (event) {
1623 	case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:	/* Cycle brightness */
1624 		brightness_switch_event(video_device, event);
1625 		keycode = KEY_BRIGHTNESS_CYCLE;
1626 		break;
1627 	case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:	/* Increase brightness */
1628 		brightness_switch_event(video_device, event);
1629 		keycode = KEY_BRIGHTNESSUP;
1630 		break;
1631 	case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:	/* Decrease brightness */
1632 		brightness_switch_event(video_device, event);
1633 		keycode = KEY_BRIGHTNESSDOWN;
1634 		break;
1635 	case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:	/* zero brightness */
1636 		brightness_switch_event(video_device, event);
1637 		keycode = KEY_BRIGHTNESS_ZERO;
1638 		break;
1639 	case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:	/* display device off */
1640 		brightness_switch_event(video_device, event);
1641 		keycode = KEY_DISPLAY_OFF;
1642 		break;
1643 	default:
1644 		acpi_handle_debug(handle, "Unsupported event [0x%x]\n", event);
1645 		break;
1646 	}
1647 
1648 	if (keycode)
1649 		may_report_brightness_keys = true;
1650 
1651 	acpi_notifier_call_chain(device, event, 0);
1652 
1653 	if (keycode && (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS)) {
1654 		input_report_key(input, keycode, 1);
1655 		input_sync(input);
1656 		input_report_key(input, keycode, 0);
1657 		input_sync(input);
1658 	}
1659 }
1660 
1661 static int acpi_video_resume(struct notifier_block *nb,
1662 				unsigned long val, void *ign)
1663 {
1664 	struct acpi_video_bus *video;
1665 	struct acpi_video_device *video_device;
1666 	int i;
1667 
1668 	switch (val) {
1669 	case PM_POST_HIBERNATION:
1670 	case PM_POST_SUSPEND:
1671 	case PM_POST_RESTORE:
1672 		video = container_of(nb, struct acpi_video_bus, pm_nb);
1673 
1674 		dev_info(&video->device->dev, "Restoring backlight state\n");
1675 
1676 		for (i = 0; i < video->attached_count; i++) {
1677 			video_device = video->attached_array[i].bind_info;
1678 			if (video_device && video_device->brightness)
1679 				acpi_video_device_lcd_set_level(video_device,
1680 						video_device->brightness->curr);
1681 		}
1682 
1683 		return NOTIFY_OK;
1684 	}
1685 	return NOTIFY_DONE;
1686 }
1687 
1688 static acpi_status
1689 acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1690 			void **return_value)
1691 {
1692 	struct acpi_device *device = context;
1693 	struct acpi_device *sibling;
1694 
1695 	if (handle == device->handle)
1696 		return AE_CTRL_TERMINATE;
1697 
1698 	sibling = acpi_fetch_acpi_dev(handle);
1699 	if (!sibling)
1700 		return AE_OK;
1701 
1702 	if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1703 			return AE_ALREADY_EXISTS;
1704 
1705 	return AE_OK;
1706 }
1707 
1708 static void acpi_video_dev_register_backlight(struct acpi_video_device *device)
1709 {
1710 	struct backlight_properties props;
1711 	struct pci_dev *pdev;
1712 	acpi_handle acpi_parent;
1713 	struct device *parent = NULL;
1714 	int result;
1715 	static int count;
1716 	char *name;
1717 
1718 	result = acpi_video_init_brightness(device);
1719 	if (result)
1720 		return;
1721 
1722 	name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
1723 	if (!name)
1724 		return;
1725 	count++;
1726 
1727 	if (ACPI_SUCCESS(acpi_get_parent(device->dev->handle, &acpi_parent))) {
1728 		pdev = acpi_get_pci_dev(acpi_parent);
1729 		if (pdev) {
1730 			parent = &pdev->dev;
1731 			pci_dev_put(pdev);
1732 		}
1733 	}
1734 
1735 	memset(&props, 0, sizeof(struct backlight_properties));
1736 	props.type = BACKLIGHT_FIRMWARE;
1737 	props.max_brightness =
1738 		device->brightness->count - ACPI_VIDEO_FIRST_LEVEL - 1;
1739 	device->backlight = backlight_device_register(name,
1740 						      parent,
1741 						      device,
1742 						      &acpi_backlight_ops,
1743 						      &props);
1744 	kfree(name);
1745 	if (IS_ERR(device->backlight)) {
1746 		device->backlight = NULL;
1747 		return;
1748 	}
1749 
1750 	/*
1751 	 * Save current brightness level in case we have to restore it
1752 	 * before acpi_video_device_lcd_set_level() is called next time.
1753 	 */
1754 	device->backlight->props.brightness =
1755 			acpi_video_get_brightness(device->backlight);
1756 
1757 	device->cooling_dev = thermal_cooling_device_register("LCD", device,
1758 							      &video_cooling_ops);
1759 	if (IS_ERR(device->cooling_dev)) {
1760 		/*
1761 		 * Set cooling_dev to NULL so we don't crash trying to free it.
1762 		 * Also, why the hell we are returning early and not attempt to
1763 		 * register video output if cooling device registration failed?
1764 		 * -- dtor
1765 		 */
1766 		device->cooling_dev = NULL;
1767 		return;
1768 	}
1769 
1770 	dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1771 		 device->cooling_dev->id);
1772 	result = sysfs_create_link(&device->dev->dev.kobj,
1773 			&device->cooling_dev->device.kobj,
1774 			"thermal_cooling");
1775 	if (result)
1776 		pr_info("sysfs link creation failed\n");
1777 
1778 	result = sysfs_create_link(&device->cooling_dev->device.kobj,
1779 			&device->dev->dev.kobj, "device");
1780 	if (result)
1781 		pr_info("Reverse sysfs link creation failed\n");
1782 }
1783 
1784 static void acpi_video_run_bcl_for_osi(struct acpi_video_bus *video)
1785 {
1786 	struct acpi_video_device *dev;
1787 	union acpi_object *levels;
1788 
1789 	mutex_lock(&video->device_list_lock);
1790 	list_for_each_entry(dev, &video->video_device_list, entry) {
1791 		if (!acpi_video_device_lcd_query_levels(dev->dev->handle, &levels))
1792 			kfree(levels);
1793 	}
1794 	mutex_unlock(&video->device_list_lock);
1795 }
1796 
1797 static bool acpi_video_should_register_backlight(struct acpi_video_device *dev)
1798 {
1799 	/*
1800 	 * Do not create backlight device for video output
1801 	 * device that is not in the enumerated list.
1802 	 */
1803 	if (!acpi_video_device_in_dod(dev)) {
1804 		dev_dbg(&dev->dev->dev, "not in _DOD list, ignore\n");
1805 		return false;
1806 	}
1807 
1808 	if (only_lcd)
1809 		return dev->flags.lcd;
1810 	return true;
1811 }
1812 
1813 static int acpi_video_bus_register_backlight(struct acpi_video_bus *video)
1814 {
1815 	struct acpi_video_device *dev;
1816 
1817 	if (video->backlight_registered)
1818 		return 0;
1819 
1820 	if (acpi_video_get_backlight_type() != acpi_backlight_video)
1821 		return 0;
1822 
1823 	mutex_lock(&video->device_list_lock);
1824 	list_for_each_entry(dev, &video->video_device_list, entry) {
1825 		if (acpi_video_should_register_backlight(dev))
1826 			acpi_video_dev_register_backlight(dev);
1827 	}
1828 	mutex_unlock(&video->device_list_lock);
1829 
1830 	video->backlight_registered = true;
1831 
1832 	video->pm_nb.notifier_call = acpi_video_resume;
1833 	video->pm_nb.priority = 0;
1834 	return register_pm_notifier(&video->pm_nb);
1835 }
1836 
1837 static void acpi_video_dev_unregister_backlight(struct acpi_video_device *device)
1838 {
1839 	if (device->backlight) {
1840 		backlight_device_unregister(device->backlight);
1841 		device->backlight = NULL;
1842 	}
1843 	if (device->brightness) {
1844 		kfree(device->brightness->levels);
1845 		kfree(device->brightness);
1846 		device->brightness = NULL;
1847 	}
1848 	if (device->cooling_dev) {
1849 		sysfs_remove_link(&device->dev->dev.kobj, "thermal_cooling");
1850 		sysfs_remove_link(&device->cooling_dev->device.kobj, "device");
1851 		thermal_cooling_device_unregister(device->cooling_dev);
1852 		device->cooling_dev = NULL;
1853 	}
1854 }
1855 
1856 static int acpi_video_bus_unregister_backlight(struct acpi_video_bus *video)
1857 {
1858 	struct acpi_video_device *dev;
1859 	int error;
1860 
1861 	if (!video->backlight_registered)
1862 		return 0;
1863 
1864 	error = unregister_pm_notifier(&video->pm_nb);
1865 
1866 	mutex_lock(&video->device_list_lock);
1867 	list_for_each_entry(dev, &video->video_device_list, entry)
1868 		acpi_video_dev_unregister_backlight(dev);
1869 	mutex_unlock(&video->device_list_lock);
1870 
1871 	video->backlight_registered = false;
1872 
1873 	return error;
1874 }
1875 
1876 static void acpi_video_dev_add_notify_handler(struct acpi_video_device *device)
1877 {
1878 	acpi_status status;
1879 	struct acpi_device *adev = device->dev;
1880 
1881 	status = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
1882 					     acpi_video_device_notify, device);
1883 	if (ACPI_FAILURE(status))
1884 		dev_err(&adev->dev, "Error installing notify handler\n");
1885 	else
1886 		device->flags.notify = 1;
1887 }
1888 
1889 static int acpi_video_bus_add_notify_handler(struct acpi_video_bus *video,
1890 					     struct platform_device *pdev)
1891 {
1892 	struct input_dev *input;
1893 	struct acpi_video_device *dev;
1894 	int error;
1895 
1896 	video->input = input = input_allocate_device();
1897 	if (!input) {
1898 		error = -ENOMEM;
1899 		goto out;
1900 	}
1901 
1902 	error = acpi_video_bus_start_devices(video);
1903 	if (error)
1904 		goto err_free_input;
1905 
1906 	snprintf(video->phys, sizeof(video->phys),
1907 			"%s/video/input0", acpi_device_hid(video->device));
1908 
1909 	input->name = acpi_device_name(video->device);
1910 	input->phys = video->phys;
1911 	input->id.bustype = BUS_HOST;
1912 	input->id.product = 0x06;
1913 	input->dev.parent = &pdev->dev;
1914 	input->evbit[0] = BIT(EV_KEY);
1915 	set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1916 	set_bit(KEY_VIDEO_NEXT, input->keybit);
1917 	set_bit(KEY_VIDEO_PREV, input->keybit);
1918 	set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1919 	set_bit(KEY_BRIGHTNESSUP, input->keybit);
1920 	set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1921 	set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1922 	set_bit(KEY_DISPLAY_OFF, input->keybit);
1923 
1924 	error = input_register_device(input);
1925 	if (error)
1926 		goto err_stop_dev;
1927 
1928 	mutex_lock(&video->device_list_lock);
1929 	list_for_each_entry(dev, &video->video_device_list, entry)
1930 		acpi_video_dev_add_notify_handler(dev);
1931 	mutex_unlock(&video->device_list_lock);
1932 
1933 	return 0;
1934 
1935 err_stop_dev:
1936 	acpi_video_bus_stop_devices(video);
1937 err_free_input:
1938 	input_free_device(input);
1939 	video->input = NULL;
1940 out:
1941 	return error;
1942 }
1943 
1944 static void acpi_video_dev_remove_notify_handler(struct acpi_video_device *dev)
1945 {
1946 	if (dev->flags.notify) {
1947 		acpi_remove_notify_handler(dev->dev->handle, ACPI_DEVICE_NOTIFY,
1948 					   acpi_video_device_notify);
1949 		dev->flags.notify = 0;
1950 	}
1951 }
1952 
1953 static void acpi_video_bus_remove_notify_handler(struct acpi_video_bus *video)
1954 {
1955 	struct acpi_video_device *dev;
1956 
1957 	mutex_lock(&video->device_list_lock);
1958 	list_for_each_entry(dev, &video->video_device_list, entry) {
1959 		acpi_video_dev_remove_notify_handler(dev);
1960 		cancel_delayed_work_sync(&dev->switch_brightness_work);
1961 	}
1962 	mutex_unlock(&video->device_list_lock);
1963 
1964 	acpi_video_bus_stop_devices(video);
1965 	input_unregister_device(video->input);
1966 	video->input = NULL;
1967 }
1968 
1969 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1970 {
1971 	struct acpi_video_device *dev, *next;
1972 
1973 	mutex_lock(&video->device_list_lock);
1974 	list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1975 		list_del(&dev->entry);
1976 		kfree(dev);
1977 	}
1978 	mutex_unlock(&video->device_list_lock);
1979 
1980 	return 0;
1981 }
1982 
1983 static int instance;
1984 
1985 static int acpi_video_bus_probe(struct platform_device *pdev)
1986 {
1987 	struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
1988 	struct acpi_video_bus *video;
1989 	bool auto_detect;
1990 	int error;
1991 	acpi_status status;
1992 
1993 	status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1994 				acpi_dev_parent(device)->handle, 1,
1995 				acpi_video_bus_match, NULL,
1996 				device, NULL);
1997 	if (status == AE_ALREADY_EXISTS) {
1998 		pr_info(FW_BUG
1999 			"Duplicate ACPI video bus devices for the"
2000 			" same VGA controller, please try module "
2001 			"parameter \"video.allow_duplicates=1\""
2002 			"if the current driver doesn't work.\n");
2003 		if (!allow_duplicates)
2004 			return -ENODEV;
2005 	}
2006 
2007 	video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
2008 	if (!video)
2009 		return -ENOMEM;
2010 
2011 	/* a hack to fix the duplicate name "VID" problem on T61 */
2012 	if (!strcmp(device->pnp.bus_id, "VID")) {
2013 		if (instance)
2014 			device->pnp.bus_id[3] = '0' + instance;
2015 		instance++;
2016 	}
2017 	/* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2018 	if (!strcmp(device->pnp.bus_id, "VGA")) {
2019 		if (instance)
2020 			device->pnp.bus_id[3] = '0' + instance;
2021 		instance++;
2022 	}
2023 
2024 	platform_set_drvdata(pdev, video);
2025 
2026 	video->device = device;
2027 	strscpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2028 	strscpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
2029 	device->driver_data = video;
2030 
2031 	acpi_video_bus_find_cap(video);
2032 	error = acpi_video_bus_check(video);
2033 	if (error)
2034 		goto err_free_video;
2035 
2036 	mutex_init(&video->device_list_lock);
2037 	INIT_LIST_HEAD(&video->video_device_list);
2038 
2039 	error = acpi_video_bus_get_devices(video, device);
2040 	if (error)
2041 		goto err_put_video;
2042 
2043 	/*
2044 	 * HP ZBook Fury 16 G10 requires ACPI video's child devices have _PS0
2045 	 * evaluated to have functional panel brightness control.
2046 	 */
2047 	acpi_device_fix_up_power_children(device);
2048 
2049 	pr_info("%s [%s] (multi-head: %s  rom: %s  post: %s)\n",
2050 	       ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2051 	       str_yes_no(video->flags.multihead),
2052 	       str_yes_no(video->flags.rom),
2053 	       str_yes_no(video->flags.post));
2054 	mutex_lock(&video_list_lock);
2055 	list_add_tail(&video->entry, &video_bus_head);
2056 	mutex_unlock(&video_list_lock);
2057 
2058 	/*
2059 	 * If backlight-type auto-detection is used then a native backlight may
2060 	 * show up later and this may change the result from video to native.
2061 	 * Therefor normally the userspace visible /sys/class/backlight device
2062 	 * gets registered separately by the GPU driver calling
2063 	 * acpi_video_register_backlight() when an internal panel is detected.
2064 	 * Register the backlight now when not using auto-detection, so that
2065 	 * when the kernel cmdline or DMI-quirks are used the backlight will
2066 	 * get registered even if acpi_video_register_backlight() is not called.
2067 	 */
2068 	acpi_video_run_bcl_for_osi(video);
2069 	if (__acpi_video_get_backlight_type(false, &auto_detect) == acpi_backlight_video &&
2070 	    !auto_detect)
2071 		acpi_video_bus_register_backlight(video);
2072 
2073 	error = acpi_video_bus_add_notify_handler(video, pdev);
2074 	if (error)
2075 		goto err_del;
2076 
2077 	error = acpi_dev_install_notify_handler(device, ACPI_DEVICE_NOTIFY,
2078 						acpi_video_bus_notify, video);
2079 	if (error)
2080 		goto err_remove;
2081 
2082 	return 0;
2083 
2084 err_remove:
2085 	acpi_video_bus_remove_notify_handler(video);
2086 err_del:
2087 	mutex_lock(&video_list_lock);
2088 	list_del(&video->entry);
2089 	mutex_unlock(&video_list_lock);
2090 	acpi_video_bus_unregister_backlight(video);
2091 err_put_video:
2092 	acpi_video_bus_put_devices(video);
2093 	kfree(video->attached_array);
2094 err_free_video:
2095 	kfree(video);
2096 	device->driver_data = NULL;
2097 
2098 	return error;
2099 }
2100 
2101 static void acpi_video_bus_remove(struct platform_device *pdev)
2102 {
2103 	struct acpi_video_bus *video = platform_get_drvdata(pdev);
2104 	struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
2105 
2106 	acpi_dev_remove_notify_handler(device, ACPI_DEVICE_NOTIFY,
2107 				       acpi_video_bus_notify);
2108 
2109 	mutex_lock(&video_list_lock);
2110 	list_del(&video->entry);
2111 	mutex_unlock(&video_list_lock);
2112 
2113 	acpi_video_bus_remove_notify_handler(video);
2114 	acpi_video_bus_unregister_backlight(video);
2115 	acpi_video_bus_put_devices(video);
2116 
2117 	kfree(video->attached_array);
2118 	kfree(video);
2119 }
2120 
2121 static int __init is_i740(struct pci_dev *dev)
2122 {
2123 	if (dev->device == 0x00D1)
2124 		return 1;
2125 	if (dev->device == 0x7000)
2126 		return 1;
2127 	return 0;
2128 }
2129 
2130 static int __init intel_opregion_present(void)
2131 {
2132 	int opregion = 0;
2133 	struct pci_dev *dev = NULL;
2134 	u32 address;
2135 
2136 	for_each_pci_dev(dev) {
2137 		if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2138 			continue;
2139 		if (dev->vendor != PCI_VENDOR_ID_INTEL)
2140 			continue;
2141 		/* We don't want to poke around undefined i740 registers */
2142 		if (is_i740(dev))
2143 			continue;
2144 		pci_read_config_dword(dev, 0xfc, &address);
2145 		if (!address)
2146 			continue;
2147 		opregion = 1;
2148 	}
2149 	return opregion;
2150 }
2151 
2152 int acpi_video_register(void)
2153 {
2154 	int ret = 0;
2155 
2156 	mutex_lock(&register_count_mutex);
2157 	if (register_count) {
2158 		/*
2159 		 * if the function of acpi_video_register is already called,
2160 		 * don't register the acpi_video_bus again and return no error.
2161 		 */
2162 		goto leave;
2163 	}
2164 
2165 	dmi_check_system(video_dmi_table);
2166 
2167 	ret = platform_driver_register(&acpi_video_bus);
2168 	if (ret)
2169 		goto leave;
2170 
2171 	/*
2172 	 * When the acpi_video_bus is loaded successfully, increase
2173 	 * the counter reference.
2174 	 */
2175 	register_count = 1;
2176 
2177 leave:
2178 	mutex_unlock(&register_count_mutex);
2179 	return ret;
2180 }
2181 EXPORT_SYMBOL(acpi_video_register);
2182 
2183 void acpi_video_unregister(void)
2184 {
2185 	mutex_lock(&register_count_mutex);
2186 	if (register_count) {
2187 		platform_driver_unregister(&acpi_video_bus);
2188 		register_count = 0;
2189 		may_report_brightness_keys = false;
2190 	}
2191 	mutex_unlock(&register_count_mutex);
2192 }
2193 EXPORT_SYMBOL(acpi_video_unregister);
2194 
2195 void acpi_video_register_backlight(void)
2196 {
2197 	struct acpi_video_bus *video;
2198 
2199 	mutex_lock(&video_list_lock);
2200 	list_for_each_entry(video, &video_bus_head, entry)
2201 		acpi_video_bus_register_backlight(video);
2202 	mutex_unlock(&video_list_lock);
2203 }
2204 EXPORT_SYMBOL(acpi_video_register_backlight);
2205 
2206 bool acpi_video_handles_brightness_key_presses(void)
2207 {
2208 	return may_report_brightness_keys &&
2209 	       (report_key_events & REPORT_BRIGHTNESS_KEY_EVENTS);
2210 }
2211 EXPORT_SYMBOL(acpi_video_handles_brightness_key_presses);
2212 
2213 /*
2214  * This is kind of nasty. Hardware using Intel chipsets may require
2215  * the video opregion code to be run first in order to initialise
2216  * state before any ACPI video calls are made. To handle this we defer
2217  * registration of the video class until the opregion code has run.
2218  */
2219 
2220 static int __init acpi_video_init(void)
2221 {
2222 	/*
2223 	 * Let the module load even if ACPI is disabled (e.g. due to
2224 	 * a broken BIOS) so that i915.ko can still be loaded on such
2225 	 * old systems without an AcpiOpRegion.
2226 	 *
2227 	 * acpi_video_register() will report -ENODEV later as well due
2228 	 * to acpi_disabled when i915.ko tries to register itself afterwards.
2229 	 */
2230 	if (acpi_disabled)
2231 		return 0;
2232 
2233 	if (intel_opregion_present())
2234 		return 0;
2235 
2236 	return acpi_video_register();
2237 }
2238 
2239 static void __exit acpi_video_exit(void)
2240 {
2241 	acpi_video_unregister();
2242 }
2243 
2244 module_init(acpi_video_init);
2245 module_exit(acpi_video_exit);
2246