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