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