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