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