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