Linux 3.12.39
[linux/fpc-iii.git] / drivers / acpi / video.c
blobff5ec8ecc257452f778f7ec2cd05b077874e3c69
1 /*
2 * video.c - ACPI Video Driver
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>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 <asm/uaccess.h>
41 #include <linux/dmi.h>
42 #include <acpi/acpi_bus.h>
43 #include <acpi/acpi_drivers.h>
44 #include <linux/suspend.h>
45 #include <acpi/video.h>
47 #include "internal.h"
49 #define PREFIX "ACPI: "
51 #define ACPI_VIDEO_BUS_NAME "Video Bus"
52 #define ACPI_VIDEO_DEVICE_NAME "Video Device"
53 #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
54 #define ACPI_VIDEO_NOTIFY_PROBE 0x81
55 #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
56 #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
57 #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
59 #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
60 #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
61 #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
62 #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
63 #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
65 #define MAX_NAME_LEN 20
67 #define _COMPONENT ACPI_VIDEO_COMPONENT
68 ACPI_MODULE_NAME("video");
70 MODULE_AUTHOR("Bruno Ducrot");
71 MODULE_DESCRIPTION("ACPI Video Driver");
72 MODULE_LICENSE("GPL");
74 static bool brightness_switch_enabled = 1;
75 module_param(brightness_switch_enabled, bool, 0644);
78 * By default, we don't allow duplicate ACPI video bus devices
79 * under the same VGA controller
81 static bool allow_duplicates;
82 module_param(allow_duplicates, bool, 0644);
84 static int register_count;
85 static int acpi_video_bus_add(struct acpi_device *device);
86 static int acpi_video_bus_remove(struct acpi_device *device);
87 static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
89 static const struct acpi_device_id video_device_ids[] = {
90 {ACPI_VIDEO_HID, 0},
91 {"", 0},
93 MODULE_DEVICE_TABLE(acpi, video_device_ids);
95 static struct acpi_driver acpi_video_bus = {
96 .name = "video",
97 .class = ACPI_VIDEO_CLASS,
98 .ids = video_device_ids,
99 .ops = {
100 .add = acpi_video_bus_add,
101 .remove = acpi_video_bus_remove,
102 .notify = acpi_video_bus_notify,
106 struct acpi_video_bus_flags {
107 u8 multihead:1; /* can switch video heads */
108 u8 rom:1; /* can retrieve a video rom */
109 u8 post:1; /* can configure the head to */
110 u8 reserved:5;
113 struct acpi_video_bus_cap {
114 u8 _DOS:1; /* Enable/Disable output switching */
115 u8 _DOD:1; /* Enumerate all devices attached to display adapter */
116 u8 _ROM:1; /* Get ROM Data */
117 u8 _GPD:1; /* Get POST Device */
118 u8 _SPD:1; /* Set POST Device */
119 u8 _VPO:1; /* Video POST Options */
120 u8 reserved:2;
123 struct acpi_video_device_attrib {
124 u32 display_index:4; /* A zero-based instance of the Display */
125 u32 display_port_attachment:4; /* This field differentiates the display type */
126 u32 display_type:4; /* Describe the specific type in use */
127 u32 vendor_specific:4; /* Chipset Vendor Specific */
128 u32 bios_can_detect:1; /* BIOS can detect the device */
129 u32 depend_on_vga:1; /* Non-VGA output device whose power is related to
130 the VGA device. */
131 u32 pipe_id:3; /* For VGA multiple-head devices. */
132 u32 reserved:10; /* Must be 0 */
133 u32 device_id_scheme:1; /* Device ID Scheme */
136 struct acpi_video_enumerated_device {
137 union {
138 u32 int_val;
139 struct acpi_video_device_attrib attrib;
140 } value;
141 struct acpi_video_device *bind_info;
144 struct acpi_video_bus {
145 struct acpi_device *device;
146 u8 dos_setting;
147 struct acpi_video_enumerated_device *attached_array;
148 u8 attached_count;
149 struct acpi_video_bus_cap cap;
150 struct acpi_video_bus_flags flags;
151 struct list_head video_device_list;
152 struct mutex device_list_lock; /* protects video_device_list */
153 struct input_dev *input;
154 char phys[32]; /* for input device */
155 struct notifier_block pm_nb;
158 struct acpi_video_device_flags {
159 u8 crt:1;
160 u8 lcd:1;
161 u8 tvout:1;
162 u8 dvi:1;
163 u8 bios:1;
164 u8 unknown:1;
165 u8 notify:1;
166 u8 reserved:1;
169 struct acpi_video_device_cap {
170 u8 _ADR:1; /* Return the unique ID */
171 u8 _BCL:1; /* Query list of brightness control levels supported */
172 u8 _BCM:1; /* Set the brightness level */
173 u8 _BQC:1; /* Get current brightness level */
174 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
175 u8 _DDC:1; /* Return the EDID for this device */
178 struct acpi_video_brightness_flags {
179 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
180 u8 _BCL_reversed:1; /* _BCL package is in a reversed order */
181 u8 _BQC_use_index:1; /* _BQC returns an index value */
184 struct acpi_video_device_brightness {
185 int curr;
186 int count;
187 int *levels;
188 struct acpi_video_brightness_flags flags;
191 struct acpi_video_device {
192 unsigned long device_id;
193 struct acpi_video_device_flags flags;
194 struct acpi_video_device_cap cap;
195 struct list_head entry;
196 struct acpi_video_bus *video;
197 struct acpi_device *dev;
198 struct acpi_video_device_brightness *brightness;
199 struct backlight_device *backlight;
200 struct thermal_cooling_device *cooling_dev;
203 static const char device_decode[][30] = {
204 "motherboard VGA device",
205 "PCI VGA device",
206 "AGP VGA device",
207 "UNKNOWN",
210 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
211 static void acpi_video_device_rebind(struct acpi_video_bus *video);
212 static void acpi_video_device_bind(struct acpi_video_bus *video,
213 struct acpi_video_device *device);
214 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
215 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
216 int level);
217 static int acpi_video_device_lcd_get_level_current(
218 struct acpi_video_device *device,
219 unsigned long long *level, bool raw);
220 static int acpi_video_get_next_level(struct acpi_video_device *device,
221 u32 level_current, u32 event);
222 static int acpi_video_switch_brightness(struct acpi_video_device *device,
223 int event);
225 /* backlight device sysfs support */
226 static int acpi_video_get_brightness(struct backlight_device *bd)
228 unsigned long long cur_level;
229 int i;
230 struct acpi_video_device *vd = bl_get_data(bd);
232 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, false))
233 return -EINVAL;
234 for (i = 2; i < vd->brightness->count; i++) {
235 if (vd->brightness->levels[i] == cur_level)
237 * The first two entries are special - see page 575
238 * of the ACPI spec 3.0
240 return i - 2;
242 return 0;
245 static int acpi_video_set_brightness(struct backlight_device *bd)
247 int request_level = bd->props.brightness + 2;
248 struct acpi_video_device *vd = bl_get_data(bd);
250 return acpi_video_device_lcd_set_level(vd,
251 vd->brightness->levels[request_level]);
254 static const struct backlight_ops acpi_backlight_ops = {
255 .get_brightness = acpi_video_get_brightness,
256 .update_status = acpi_video_set_brightness,
259 /* thermal cooling device callbacks */
260 static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
261 long *state)
263 struct acpi_device *device = cooling_dev->devdata;
264 struct acpi_video_device *video = acpi_driver_data(device);
266 *state = video->brightness->count - 3;
267 return 0;
270 static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
271 long *state)
273 struct acpi_device *device = cooling_dev->devdata;
274 struct acpi_video_device *video = acpi_driver_data(device);
275 unsigned long long level;
276 int offset;
278 if (acpi_video_device_lcd_get_level_current(video, &level, false))
279 return -EINVAL;
280 for (offset = 2; offset < video->brightness->count; offset++)
281 if (level == video->brightness->levels[offset]) {
282 *state = video->brightness->count - offset - 1;
283 return 0;
286 return -EINVAL;
289 static int
290 video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
292 struct acpi_device *device = cooling_dev->devdata;
293 struct acpi_video_device *video = acpi_driver_data(device);
294 int level;
296 if (state >= video->brightness->count - 2)
297 return -EINVAL;
299 state = video->brightness->count - state;
300 level = video->brightness->levels[state - 1];
301 return acpi_video_device_lcd_set_level(video, level);
304 static const struct thermal_cooling_device_ops video_cooling_ops = {
305 .get_max_state = video_get_max_state,
306 .get_cur_state = video_get_cur_state,
307 .set_cur_state = video_set_cur_state,
311 * --------------------------------------------------------------------------
312 * Video Management
313 * --------------------------------------------------------------------------
316 static int
317 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
318 union acpi_object **levels)
320 int status;
321 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
322 union acpi_object *obj;
325 *levels = NULL;
327 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
328 if (!ACPI_SUCCESS(status))
329 return status;
330 obj = (union acpi_object *)buffer.pointer;
331 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
332 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
333 status = -EFAULT;
334 goto err;
337 *levels = obj;
339 return 0;
341 err:
342 kfree(buffer.pointer);
344 return status;
347 static int
348 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
350 int status;
351 int state;
353 status = acpi_execute_simple_method(device->dev->handle,
354 "_BCM", level);
355 if (ACPI_FAILURE(status)) {
356 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
357 return -EIO;
360 device->brightness->curr = level;
361 for (state = 2; state < device->brightness->count; state++)
362 if (level == device->brightness->levels[state]) {
363 if (device->backlight)
364 device->backlight->props.brightness = state - 2;
365 return 0;
368 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
369 return -EINVAL;
373 * For some buggy _BQC methods, we need to add a constant value to
374 * the _BQC return value to get the actual current brightness level
377 static int bqc_offset_aml_bug_workaround;
378 static int __init video_set_bqc_offset(const struct dmi_system_id *d)
380 bqc_offset_aml_bug_workaround = 9;
381 return 0;
384 static struct dmi_system_id video_dmi_table[] __initdata = {
386 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
389 .callback = video_set_bqc_offset,
390 .ident = "Acer Aspire 5720",
391 .matches = {
392 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
393 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
397 .callback = video_set_bqc_offset,
398 .ident = "Acer Aspire 5710Z",
399 .matches = {
400 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
401 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
405 .callback = video_set_bqc_offset,
406 .ident = "eMachines E510",
407 .matches = {
408 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
409 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
413 .callback = video_set_bqc_offset,
414 .ident = "Acer Aspire 5315",
415 .matches = {
416 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
417 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
421 .callback = video_set_bqc_offset,
422 .ident = "Acer Aspire 7720",
423 .matches = {
424 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
425 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
431 static unsigned long long
432 acpi_video_bqc_value_to_level(struct acpi_video_device *device,
433 unsigned long long bqc_value)
435 unsigned long long level;
437 if (device->brightness->flags._BQC_use_index) {
439 * _BQC returns an index that doesn't account for
440 * the first 2 items with special meaning, so we need
441 * to compensate for that by offsetting ourselves
443 if (device->brightness->flags._BCL_reversed)
444 bqc_value = device->brightness->count - 3 - bqc_value;
446 level = device->brightness->levels[bqc_value + 2];
447 } else {
448 level = bqc_value;
451 level += bqc_offset_aml_bug_workaround;
453 return level;
456 static int
457 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
458 unsigned long long *level, bool raw)
460 acpi_status status = AE_OK;
461 int i;
463 if (device->cap._BQC || device->cap._BCQ) {
464 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
466 status = acpi_evaluate_integer(device->dev->handle, buf,
467 NULL, level);
468 if (ACPI_SUCCESS(status)) {
469 if (raw) {
471 * Caller has indicated he wants the raw
472 * value returned by _BQC, so don't furtherly
473 * mess with the value.
475 return 0;
478 *level = acpi_video_bqc_value_to_level(device, *level);
480 for (i = 2; i < device->brightness->count; i++)
481 if (device->brightness->levels[i] == *level) {
482 device->brightness->curr = *level;
483 return 0;
486 * BQC returned an invalid level.
487 * Stop using it.
489 ACPI_WARNING((AE_INFO,
490 "%s returned an invalid level",
491 buf));
492 device->cap._BQC = device->cap._BCQ = 0;
493 } else {
495 * Fixme:
496 * should we return an error or ignore this failure?
497 * dev->brightness->curr is a cached value which stores
498 * the correct current backlight level in most cases.
499 * ACPI video backlight still works w/ buggy _BQC.
500 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
502 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
503 device->cap._BQC = device->cap._BCQ = 0;
507 *level = device->brightness->curr;
508 return 0;
511 static int
512 acpi_video_device_EDID(struct acpi_video_device *device,
513 union acpi_object **edid, ssize_t length)
515 int status;
516 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
517 union acpi_object *obj;
518 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
519 struct acpi_object_list args = { 1, &arg0 };
522 *edid = NULL;
524 if (!device)
525 return -ENODEV;
526 if (length == 128)
527 arg0.integer.value = 1;
528 else if (length == 256)
529 arg0.integer.value = 2;
530 else
531 return -EINVAL;
533 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
534 if (ACPI_FAILURE(status))
535 return -ENODEV;
537 obj = buffer.pointer;
539 if (obj && obj->type == ACPI_TYPE_BUFFER)
540 *edid = obj;
541 else {
542 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
543 status = -EFAULT;
544 kfree(obj);
547 return status;
550 /* bus */
553 * Arg:
554 * video : video bus device pointer
555 * bios_flag :
556 * 0. The system BIOS should NOT automatically switch(toggle)
557 * the active display output.
558 * 1. The system BIOS should automatically switch (toggle) the
559 * active display output. No switch event.
560 * 2. The _DGS value should be locked.
561 * 3. The system BIOS should not automatically switch (toggle) the
562 * active display output, but instead generate the display switch
563 * event notify code.
564 * lcd_flag :
565 * 0. The system BIOS should automatically control the brightness level
566 * of the LCD when the power changes from AC to DC
567 * 1. The system BIOS should NOT automatically control the brightness
568 * level of the LCD when the power changes from AC to DC.
569 * Return Value:
570 * -EINVAL wrong arg.
573 static int
574 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
576 acpi_status status;
578 if (!video->cap._DOS)
579 return 0;
581 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
582 return -EINVAL;
583 video->dos_setting = (lcd_flag << 2) | bios_flag;
584 status = acpi_execute_simple_method(video->device->handle, "_DOS",
585 (lcd_flag << 2) | bios_flag);
586 if (ACPI_FAILURE(status))
587 return -EIO;
589 return 0;
593 * Simple comparison function used to sort backlight levels.
596 static int
597 acpi_video_cmp_level(const void *a, const void *b)
599 return *(int *)a - *(int *)b;
603 * Decides if _BQC/_BCQ for this system is usable
605 * We do this by changing the level first and then read out the current
606 * brightness level, if the value does not match, find out if it is using
607 * index. If not, clear the _BQC/_BCQ capability.
609 static int acpi_video_bqc_quirk(struct acpi_video_device *device,
610 int max_level, int current_level)
612 struct acpi_video_device_brightness *br = device->brightness;
613 int result;
614 unsigned long long level;
615 int test_level;
617 /* don't mess with existing known broken systems */
618 if (bqc_offset_aml_bug_workaround)
619 return 0;
622 * Some systems always report current brightness level as maximum
623 * through _BQC, we need to test another value for them.
625 test_level = current_level == max_level ? br->levels[3] : max_level;
627 result = acpi_video_device_lcd_set_level(device, test_level);
628 if (result)
629 return result;
631 result = acpi_video_device_lcd_get_level_current(device, &level, true);
632 if (result)
633 return result;
635 if (level != test_level) {
636 /* buggy _BQC found, need to find out if it uses index */
637 if (level < br->count) {
638 if (br->flags._BCL_reversed)
639 level = br->count - 3 - level;
640 if (br->levels[level + 2] == test_level)
641 br->flags._BQC_use_index = 1;
644 if (!br->flags._BQC_use_index)
645 device->cap._BQC = device->cap._BCQ = 0;
648 return 0;
653 * Arg:
654 * device : video output device (LCD, CRT, ..)
656 * Return Value:
657 * Maximum brightness level
659 * Allocate and initialize device->brightness.
662 static int
663 acpi_video_init_brightness(struct acpi_video_device *device)
665 union acpi_object *obj = NULL;
666 int i, max_level = 0, count = 0, level_ac_battery = 0;
667 unsigned long long level, level_old;
668 union acpi_object *o;
669 struct acpi_video_device_brightness *br = NULL;
670 int result = -EINVAL;
671 u32 value;
673 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
674 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
675 "LCD brightness level\n"));
676 goto out;
679 if (obj->package.count < 2)
680 goto out;
682 br = kzalloc(sizeof(*br), GFP_KERNEL);
683 if (!br) {
684 printk(KERN_ERR "can't allocate memory\n");
685 result = -ENOMEM;
686 goto out;
689 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
690 GFP_KERNEL);
691 if (!br->levels) {
692 result = -ENOMEM;
693 goto out_free;
696 for (i = 0; i < obj->package.count; i++) {
697 o = (union acpi_object *)&obj->package.elements[i];
698 if (o->type != ACPI_TYPE_INTEGER) {
699 printk(KERN_ERR PREFIX "Invalid data\n");
700 continue;
702 value = (u32) o->integer.value;
703 /* Skip duplicate entries */
704 if (count > 2 && br->levels[count - 1] == value)
705 continue;
707 br->levels[count] = value;
709 if (br->levels[count] > max_level)
710 max_level = br->levels[count];
711 count++;
715 * some buggy BIOS don't export the levels
716 * when machine is on AC/Battery in _BCL package.
717 * In this case, the first two elements in _BCL packages
718 * are also supported brightness levels that OS should take care of.
720 for (i = 2; i < count; i++) {
721 if (br->levels[i] == br->levels[0])
722 level_ac_battery++;
723 if (br->levels[i] == br->levels[1])
724 level_ac_battery++;
727 if (level_ac_battery < 2) {
728 level_ac_battery = 2 - level_ac_battery;
729 br->flags._BCL_no_ac_battery_levels = 1;
730 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
731 br->levels[i] = br->levels[i - level_ac_battery];
732 count += level_ac_battery;
733 } else if (level_ac_battery > 2)
734 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package"));
736 /* Check if the _BCL package is in a reversed order */
737 if (max_level == br->levels[2]) {
738 br->flags._BCL_reversed = 1;
739 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
740 acpi_video_cmp_level, NULL);
741 } else if (max_level != br->levels[count - 1])
742 ACPI_ERROR((AE_INFO,
743 "Found unordered _BCL package"));
745 br->count = count;
746 device->brightness = br;
748 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
749 br->curr = level = max_level;
751 if (!device->cap._BQC)
752 goto set_level;
754 result = acpi_video_device_lcd_get_level_current(device,
755 &level_old, true);
756 if (result)
757 goto out_free_levels;
759 result = acpi_video_bqc_quirk(device, max_level, level_old);
760 if (result)
761 goto out_free_levels;
763 * cap._BQC may get cleared due to _BQC is found to be broken
764 * in acpi_video_bqc_quirk, so check again here.
766 if (!device->cap._BQC)
767 goto set_level;
769 level = acpi_video_bqc_value_to_level(device, level_old);
771 * On some buggy laptops, _BQC returns an uninitialized
772 * value when invoked for the first time, i.e.
773 * level_old is invalid (no matter whether it's a level
774 * or an index). Set the backlight to max_level in this case.
776 for (i = 2; i < br->count; i++)
777 if (level == br->levels[i])
778 break;
779 if (i == br->count || !level)
780 level = max_level;
782 set_level:
783 result = acpi_video_device_lcd_set_level(device, level);
784 if (result)
785 goto out_free_levels;
787 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
788 "found %d brightness levels\n", count - 2));
789 kfree(obj);
790 return result;
792 out_free_levels:
793 kfree(br->levels);
794 out_free:
795 kfree(br);
796 out:
797 device->brightness = NULL;
798 kfree(obj);
799 return result;
803 * Arg:
804 * device : video output device (LCD, CRT, ..)
806 * Return Value:
807 * None
809 * Find out all required AML methods defined under the output
810 * device.
813 static void acpi_video_device_find_cap(struct acpi_video_device *device)
815 if (acpi_has_method(device->dev->handle, "_ADR"))
816 device->cap._ADR = 1;
817 if (acpi_has_method(device->dev->handle, "_BCL"))
818 device->cap._BCL = 1;
819 if (acpi_has_method(device->dev->handle, "_BCM"))
820 device->cap._BCM = 1;
821 if (acpi_has_method(device->dev->handle, "_BQC")) {
822 device->cap._BQC = 1;
823 } else if (acpi_has_method(device->dev->handle, "_BCQ")) {
824 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
825 device->cap._BCQ = 1;
828 if (acpi_has_method(device->dev->handle, "_DDC"))
829 device->cap._DDC = 1;
831 if (acpi_video_backlight_support()) {
832 struct backlight_properties props;
833 struct pci_dev *pdev;
834 acpi_handle acpi_parent;
835 struct device *parent = NULL;
836 int result;
837 static int count;
838 char *name;
840 result = acpi_video_init_brightness(device);
841 if (result)
842 return;
843 name = kasprintf(GFP_KERNEL, "acpi_video%d", count);
844 if (!name)
845 return;
846 count++;
848 acpi_get_parent(device->dev->handle, &acpi_parent);
850 pdev = acpi_get_pci_dev(acpi_parent);
851 if (pdev) {
852 parent = &pdev->dev;
853 pci_dev_put(pdev);
856 memset(&props, 0, sizeof(struct backlight_properties));
857 props.type = BACKLIGHT_FIRMWARE;
858 props.max_brightness = device->brightness->count - 3;
859 device->backlight = backlight_device_register(name,
860 parent,
861 device,
862 &acpi_backlight_ops,
863 &props);
864 kfree(name);
865 if (IS_ERR(device->backlight))
866 return;
869 * Save current brightness level in case we have to restore it
870 * before acpi_video_device_lcd_set_level() is called next time.
872 device->backlight->props.brightness =
873 acpi_video_get_brightness(device->backlight);
875 device->cooling_dev = thermal_cooling_device_register("LCD",
876 device->dev, &video_cooling_ops);
877 if (IS_ERR(device->cooling_dev)) {
879 * Set cooling_dev to NULL so we don't crash trying to
880 * free it.
881 * Also, why the hell we are returning early and
882 * not attempt to register video output if cooling
883 * device registration failed?
884 * -- dtor
886 device->cooling_dev = NULL;
887 return;
890 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
891 device->cooling_dev->id);
892 result = sysfs_create_link(&device->dev->dev.kobj,
893 &device->cooling_dev->device.kobj,
894 "thermal_cooling");
895 if (result)
896 printk(KERN_ERR PREFIX "Create sysfs link\n");
897 result = sysfs_create_link(&device->cooling_dev->device.kobj,
898 &device->dev->dev.kobj, "device");
899 if (result)
900 printk(KERN_ERR PREFIX "Create sysfs link\n");
906 * Arg:
907 * device : video output device (VGA)
909 * Return Value:
910 * None
912 * Find out all required AML methods defined under the video bus device.
915 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
917 if (acpi_has_method(video->device->handle, "_DOS"))
918 video->cap._DOS = 1;
919 if (acpi_has_method(video->device->handle, "_DOD"))
920 video->cap._DOD = 1;
921 if (acpi_has_method(video->device->handle, "_ROM"))
922 video->cap._ROM = 1;
923 if (acpi_has_method(video->device->handle, "_GPD"))
924 video->cap._GPD = 1;
925 if (acpi_has_method(video->device->handle, "_SPD"))
926 video->cap._SPD = 1;
927 if (acpi_has_method(video->device->handle, "_VPO"))
928 video->cap._VPO = 1;
932 * Check whether the video bus device has required AML method to
933 * support the desired features
936 static int acpi_video_bus_check(struct acpi_video_bus *video)
938 acpi_status status = -ENOENT;
939 struct pci_dev *dev;
941 if (!video)
942 return -EINVAL;
944 dev = acpi_get_pci_dev(video->device->handle);
945 if (!dev)
946 return -ENODEV;
947 pci_dev_put(dev);
950 * Since there is no HID, CID and so on for VGA driver, we have
951 * to check well known required nodes.
954 /* Does this device support video switching? */
955 if (video->cap._DOS || video->cap._DOD) {
956 if (!video->cap._DOS) {
957 printk(KERN_WARNING FW_BUG
958 "ACPI(%s) defines _DOD but not _DOS\n",
959 acpi_device_bid(video->device));
961 video->flags.multihead = 1;
962 status = 0;
965 /* Does this device support retrieving a video ROM? */
966 if (video->cap._ROM) {
967 video->flags.rom = 1;
968 status = 0;
971 /* Does this device support configuring which video device to POST? */
972 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
973 video->flags.post = 1;
974 status = 0;
977 return status;
981 * --------------------------------------------------------------------------
982 * Driver Interface
983 * --------------------------------------------------------------------------
986 /* device interface */
987 static struct acpi_video_device_attrib *
988 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
990 struct acpi_video_enumerated_device *ids;
991 int i;
993 for (i = 0; i < video->attached_count; i++) {
994 ids = &video->attached_array[i];
995 if ((ids->value.int_val & 0xffff) == device_id)
996 return &ids->value.attrib;
999 return NULL;
1002 static int
1003 acpi_video_get_device_type(struct acpi_video_bus *video,
1004 unsigned long device_id)
1006 struct acpi_video_enumerated_device *ids;
1007 int i;
1009 for (i = 0; i < video->attached_count; i++) {
1010 ids = &video->attached_array[i];
1011 if ((ids->value.int_val & 0xffff) == device_id)
1012 return ids->value.int_val;
1015 return 0;
1018 static int
1019 acpi_video_bus_get_one_device(struct acpi_device *device,
1020 struct acpi_video_bus *video)
1022 unsigned long long device_id;
1023 int status, device_type;
1024 struct acpi_video_device *data;
1025 struct acpi_video_device_attrib *attribute;
1027 status =
1028 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1029 /* Some device omits _ADR, we skip them instead of fail */
1030 if (ACPI_FAILURE(status))
1031 return 0;
1033 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1034 if (!data)
1035 return -ENOMEM;
1037 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1038 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1039 device->driver_data = data;
1041 data->device_id = device_id;
1042 data->video = video;
1043 data->dev = device;
1045 attribute = acpi_video_get_device_attr(video, device_id);
1047 if (attribute && attribute->device_id_scheme) {
1048 switch (attribute->display_type) {
1049 case ACPI_VIDEO_DISPLAY_CRT:
1050 data->flags.crt = 1;
1051 break;
1052 case ACPI_VIDEO_DISPLAY_TV:
1053 data->flags.tvout = 1;
1054 break;
1055 case ACPI_VIDEO_DISPLAY_DVI:
1056 data->flags.dvi = 1;
1057 break;
1058 case ACPI_VIDEO_DISPLAY_LCD:
1059 data->flags.lcd = 1;
1060 break;
1061 default:
1062 data->flags.unknown = 1;
1063 break;
1065 if (attribute->bios_can_detect)
1066 data->flags.bios = 1;
1067 } else {
1068 /* Check for legacy IDs */
1069 device_type = acpi_video_get_device_type(video, device_id);
1070 /* Ignore bits 16 and 18-20 */
1071 switch (device_type & 0xffe2ffff) {
1072 case ACPI_VIDEO_DISPLAY_LEGACY_MONITOR:
1073 data->flags.crt = 1;
1074 break;
1075 case ACPI_VIDEO_DISPLAY_LEGACY_PANEL:
1076 data->flags.lcd = 1;
1077 break;
1078 case ACPI_VIDEO_DISPLAY_LEGACY_TV:
1079 data->flags.tvout = 1;
1080 break;
1081 default:
1082 data->flags.unknown = 1;
1086 acpi_video_device_bind(video, data);
1087 acpi_video_device_find_cap(data);
1089 status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
1090 acpi_video_device_notify, data);
1091 if (ACPI_FAILURE(status))
1092 dev_err(&device->dev, "Error installing notify handler\n");
1093 else
1094 data->flags.notify = 1;
1096 mutex_lock(&video->device_list_lock);
1097 list_add_tail(&data->entry, &video->video_device_list);
1098 mutex_unlock(&video->device_list_lock);
1100 return status;
1104 * Arg:
1105 * video : video bus device
1107 * Return:
1108 * none
1110 * Enumerate the video device list of the video bus,
1111 * bind the ids with the corresponding video devices
1112 * under the video bus.
1115 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1117 struct acpi_video_device *dev;
1119 mutex_lock(&video->device_list_lock);
1121 list_for_each_entry(dev, &video->video_device_list, entry)
1122 acpi_video_device_bind(video, dev);
1124 mutex_unlock(&video->device_list_lock);
1128 * Arg:
1129 * video : video bus device
1130 * device : video output device under the video
1131 * bus
1133 * Return:
1134 * none
1136 * Bind the ids with the corresponding video devices
1137 * under the video bus.
1140 static void
1141 acpi_video_device_bind(struct acpi_video_bus *video,
1142 struct acpi_video_device *device)
1144 struct acpi_video_enumerated_device *ids;
1145 int i;
1147 for (i = 0; i < video->attached_count; i++) {
1148 ids = &video->attached_array[i];
1149 if (device->device_id == (ids->value.int_val & 0xffff)) {
1150 ids->bind_info = device;
1151 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1157 * Arg:
1158 * video : video bus device
1160 * Return:
1161 * < 0 : error
1163 * Call _DOD to enumerate all devices attached to display adapter
1167 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1169 int status;
1170 int count;
1171 int i;
1172 struct acpi_video_enumerated_device *active_list;
1173 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1174 union acpi_object *dod = NULL;
1175 union acpi_object *obj;
1177 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1178 if (!ACPI_SUCCESS(status)) {
1179 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1180 return status;
1183 dod = buffer.pointer;
1184 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1185 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1186 status = -EFAULT;
1187 goto out;
1190 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1191 dod->package.count));
1193 active_list = kcalloc(1 + dod->package.count,
1194 sizeof(struct acpi_video_enumerated_device),
1195 GFP_KERNEL);
1196 if (!active_list) {
1197 status = -ENOMEM;
1198 goto out;
1201 count = 0;
1202 for (i = 0; i < dod->package.count; i++) {
1203 obj = &dod->package.elements[i];
1205 if (obj->type != ACPI_TYPE_INTEGER) {
1206 printk(KERN_ERR PREFIX
1207 "Invalid _DOD data in element %d\n", i);
1208 continue;
1211 active_list[count].value.int_val = obj->integer.value;
1212 active_list[count].bind_info = NULL;
1213 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1214 (int)obj->integer.value));
1215 count++;
1218 kfree(video->attached_array);
1220 video->attached_array = active_list;
1221 video->attached_count = count;
1223 out:
1224 kfree(buffer.pointer);
1225 return status;
1228 static int
1229 acpi_video_get_next_level(struct acpi_video_device *device,
1230 u32 level_current, u32 event)
1232 int min, max, min_above, max_below, i, l, delta = 255;
1233 max = max_below = 0;
1234 min = min_above = 255;
1235 /* Find closest level to level_current */
1236 for (i = 2; i < device->brightness->count; i++) {
1237 l = device->brightness->levels[i];
1238 if (abs(l - level_current) < abs(delta)) {
1239 delta = l - level_current;
1240 if (!delta)
1241 break;
1244 /* Ajust level_current to closest available level */
1245 level_current += delta;
1246 for (i = 2; i < device->brightness->count; i++) {
1247 l = device->brightness->levels[i];
1248 if (l < min)
1249 min = l;
1250 if (l > max)
1251 max = l;
1252 if (l < min_above && l > level_current)
1253 min_above = l;
1254 if (l > max_below && l < level_current)
1255 max_below = l;
1258 switch (event) {
1259 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1260 return (level_current < max) ? min_above : min;
1261 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1262 return (level_current < max) ? min_above : max;
1263 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1264 return (level_current > min) ? max_below : min;
1265 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1266 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1267 return 0;
1268 default:
1269 return level_current;
1273 static int
1274 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1276 unsigned long long level_current, level_next;
1277 int result = -EINVAL;
1279 /* no warning message if acpi_backlight=vendor is used */
1280 if (!acpi_video_backlight_support())
1281 return 0;
1283 if (!device->brightness)
1284 goto out;
1286 result = acpi_video_device_lcd_get_level_current(device,
1287 &level_current,
1288 false);
1289 if (result)
1290 goto out;
1292 level_next = acpi_video_get_next_level(device, level_current, event);
1294 result = acpi_video_device_lcd_set_level(device, level_next);
1296 if (!result)
1297 backlight_force_update(device->backlight,
1298 BACKLIGHT_UPDATE_HOTKEY);
1300 out:
1301 if (result)
1302 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1304 return result;
1307 int acpi_video_get_edid(struct acpi_device *device, int type, int device_id,
1308 void **edid)
1310 struct acpi_video_bus *video;
1311 struct acpi_video_device *video_device;
1312 union acpi_object *buffer = NULL;
1313 acpi_status status;
1314 int i, length;
1316 if (!device || !acpi_driver_data(device))
1317 return -EINVAL;
1319 video = acpi_driver_data(device);
1321 for (i = 0; i < video->attached_count; i++) {
1322 video_device = video->attached_array[i].bind_info;
1323 length = 256;
1325 if (!video_device)
1326 continue;
1328 if (!video_device->cap._DDC)
1329 continue;
1331 if (type) {
1332 switch (type) {
1333 case ACPI_VIDEO_DISPLAY_CRT:
1334 if (!video_device->flags.crt)
1335 continue;
1336 break;
1337 case ACPI_VIDEO_DISPLAY_TV:
1338 if (!video_device->flags.tvout)
1339 continue;
1340 break;
1341 case ACPI_VIDEO_DISPLAY_DVI:
1342 if (!video_device->flags.dvi)
1343 continue;
1344 break;
1345 case ACPI_VIDEO_DISPLAY_LCD:
1346 if (!video_device->flags.lcd)
1347 continue;
1348 break;
1350 } else if (video_device->device_id != device_id) {
1351 continue;
1354 status = acpi_video_device_EDID(video_device, &buffer, length);
1356 if (ACPI_FAILURE(status) || !buffer ||
1357 buffer->type != ACPI_TYPE_BUFFER) {
1358 length = 128;
1359 status = acpi_video_device_EDID(video_device, &buffer,
1360 length);
1361 if (ACPI_FAILURE(status) || !buffer ||
1362 buffer->type != ACPI_TYPE_BUFFER) {
1363 continue;
1367 *edid = buffer->buffer.pointer;
1368 return length;
1371 return -ENODEV;
1373 EXPORT_SYMBOL(acpi_video_get_edid);
1375 static int
1376 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1377 struct acpi_device *device)
1379 int status = 0;
1380 struct acpi_device *dev;
1383 * There are systems where video module known to work fine regardless
1384 * of broken _DOD and ignoring returned value here doesn't cause
1385 * any issues later.
1387 acpi_video_device_enumerate(video);
1389 list_for_each_entry(dev, &device->children, node) {
1391 status = acpi_video_bus_get_one_device(dev, video);
1392 if (status) {
1393 dev_err(&dev->dev, "Can't attach device\n");
1394 break;
1397 return status;
1400 static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1402 acpi_status status;
1404 if (!device || !device->video)
1405 return -ENOENT;
1407 if (device->flags.notify) {
1408 status = acpi_remove_notify_handler(device->dev->handle,
1409 ACPI_DEVICE_NOTIFY, acpi_video_device_notify);
1410 if (ACPI_FAILURE(status))
1411 dev_err(&device->dev->dev,
1412 "Can't remove video notify handler\n");
1415 if (device->backlight) {
1416 backlight_device_unregister(device->backlight);
1417 device->backlight = NULL;
1419 if (device->cooling_dev) {
1420 sysfs_remove_link(&device->dev->dev.kobj,
1421 "thermal_cooling");
1422 sysfs_remove_link(&device->cooling_dev->device.kobj,
1423 "device");
1424 thermal_cooling_device_unregister(device->cooling_dev);
1425 device->cooling_dev = NULL;
1428 return 0;
1431 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1433 int status;
1434 struct acpi_video_device *dev, *next;
1436 mutex_lock(&video->device_list_lock);
1438 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1440 status = acpi_video_bus_put_one_device(dev);
1441 if (ACPI_FAILURE(status))
1442 printk(KERN_WARNING PREFIX
1443 "hhuuhhuu bug in acpi video driver.\n");
1445 if (dev->brightness) {
1446 kfree(dev->brightness->levels);
1447 kfree(dev->brightness);
1449 list_del(&dev->entry);
1450 kfree(dev);
1453 mutex_unlock(&video->device_list_lock);
1455 return 0;
1458 /* acpi_video interface */
1461 * Win8 requires setting bit2 of _DOS to let firmware know it shouldn't
1462 * preform any automatic brightness change on receiving a notification.
1464 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1466 return acpi_video_bus_DOS(video, 0,
1467 acpi_video_backlight_quirks() ? 1 : 0);
1470 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1472 return acpi_video_bus_DOS(video, 0,
1473 acpi_video_backlight_quirks() ? 0 : 1);
1476 static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
1478 struct acpi_video_bus *video = acpi_driver_data(device);
1479 struct input_dev *input;
1480 int keycode = 0;
1482 if (!video)
1483 return;
1485 input = video->input;
1487 switch (event) {
1488 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
1489 * most likely via hotkey. */
1490 keycode = KEY_SWITCHVIDEOMODE;
1491 break;
1493 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
1494 * connector. */
1495 acpi_video_device_enumerate(video);
1496 acpi_video_device_rebind(video);
1497 keycode = KEY_SWITCHVIDEOMODE;
1498 break;
1500 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
1501 keycode = KEY_SWITCHVIDEOMODE;
1502 break;
1503 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
1504 keycode = KEY_VIDEO_NEXT;
1505 break;
1506 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
1507 keycode = KEY_VIDEO_PREV;
1508 break;
1510 default:
1511 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1512 "Unsupported event [0x%x]\n", event));
1513 break;
1516 if (acpi_notifier_call_chain(device, event, 0))
1517 /* Something vetoed the keypress. */
1518 keycode = 0;
1520 if (keycode) {
1521 input_report_key(input, keycode, 1);
1522 input_sync(input);
1523 input_report_key(input, keycode, 0);
1524 input_sync(input);
1527 return;
1530 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1532 struct acpi_video_device *video_device = data;
1533 struct acpi_device *device = NULL;
1534 struct acpi_video_bus *bus;
1535 struct input_dev *input;
1536 int keycode = 0;
1538 if (!video_device)
1539 return;
1541 device = video_device->dev;
1542 bus = video_device->video;
1543 input = bus->input;
1545 switch (event) {
1546 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
1547 if (brightness_switch_enabled)
1548 acpi_video_switch_brightness(video_device, event);
1549 keycode = KEY_BRIGHTNESS_CYCLE;
1550 break;
1551 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
1552 if (brightness_switch_enabled)
1553 acpi_video_switch_brightness(video_device, event);
1554 keycode = KEY_BRIGHTNESSUP;
1555 break;
1556 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
1557 if (brightness_switch_enabled)
1558 acpi_video_switch_brightness(video_device, event);
1559 keycode = KEY_BRIGHTNESSDOWN;
1560 break;
1561 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightness */
1562 if (brightness_switch_enabled)
1563 acpi_video_switch_brightness(video_device, event);
1564 keycode = KEY_BRIGHTNESS_ZERO;
1565 break;
1566 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
1567 if (brightness_switch_enabled)
1568 acpi_video_switch_brightness(video_device, event);
1569 keycode = KEY_DISPLAY_OFF;
1570 break;
1571 default:
1572 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1573 "Unsupported event [0x%x]\n", event));
1574 break;
1577 acpi_notifier_call_chain(device, event, 0);
1579 if (keycode) {
1580 input_report_key(input, keycode, 1);
1581 input_sync(input);
1582 input_report_key(input, keycode, 0);
1583 input_sync(input);
1586 return;
1589 static int acpi_video_resume(struct notifier_block *nb,
1590 unsigned long val, void *ign)
1592 struct acpi_video_bus *video;
1593 struct acpi_video_device *video_device;
1594 int i;
1596 switch (val) {
1597 case PM_HIBERNATION_PREPARE:
1598 case PM_SUSPEND_PREPARE:
1599 case PM_RESTORE_PREPARE:
1600 return NOTIFY_DONE;
1603 video = container_of(nb, struct acpi_video_bus, pm_nb);
1605 dev_info(&video->device->dev, "Restoring backlight state\n");
1607 for (i = 0; i < video->attached_count; i++) {
1608 video_device = video->attached_array[i].bind_info;
1609 if (video_device && video_device->backlight)
1610 acpi_video_set_brightness(video_device->backlight);
1613 return NOTIFY_OK;
1616 static acpi_status
1617 acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
1618 void **return_value)
1620 struct acpi_device *device = context;
1621 struct acpi_device *sibling;
1622 int result;
1624 if (handle == device->handle)
1625 return AE_CTRL_TERMINATE;
1627 result = acpi_bus_get_device(handle, &sibling);
1628 if (result)
1629 return AE_OK;
1631 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
1632 return AE_ALREADY_EXISTS;
1634 return AE_OK;
1637 static int instance;
1639 static int acpi_video_bus_add(struct acpi_device *device)
1641 struct acpi_video_bus *video;
1642 struct input_dev *input;
1643 int error;
1644 acpi_status status;
1646 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
1647 device->parent->handle, 1,
1648 acpi_video_bus_match, NULL,
1649 device, NULL);
1650 if (status == AE_ALREADY_EXISTS) {
1651 printk(KERN_WARNING FW_BUG
1652 "Duplicate ACPI video bus devices for the"
1653 " same VGA controller, please try module "
1654 "parameter \"video.allow_duplicates=1\""
1655 "if the current driver doesn't work.\n");
1656 if (!allow_duplicates)
1657 return -ENODEV;
1660 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1661 if (!video)
1662 return -ENOMEM;
1664 /* a hack to fix the duplicate name "VID" problem on T61 */
1665 if (!strcmp(device->pnp.bus_id, "VID")) {
1666 if (instance)
1667 device->pnp.bus_id[3] = '0' + instance;
1668 instance++;
1670 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
1671 if (!strcmp(device->pnp.bus_id, "VGA")) {
1672 if (instance)
1673 device->pnp.bus_id[3] = '0' + instance;
1674 instance++;
1677 video->device = device;
1678 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1679 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1680 device->driver_data = video;
1682 acpi_video_bus_find_cap(video);
1683 error = acpi_video_bus_check(video);
1684 if (error)
1685 goto err_free_video;
1687 mutex_init(&video->device_list_lock);
1688 INIT_LIST_HEAD(&video->video_device_list);
1690 error = acpi_video_bus_get_devices(video, device);
1691 if (error)
1692 goto err_put_video;
1694 video->input = input = input_allocate_device();
1695 if (!input) {
1696 error = -ENOMEM;
1697 goto err_put_video;
1700 error = acpi_video_bus_start_devices(video);
1701 if (error)
1702 goto err_free_input_dev;
1704 snprintf(video->phys, sizeof(video->phys),
1705 "%s/video/input0", acpi_device_hid(video->device));
1707 input->name = acpi_device_name(video->device);
1708 input->phys = video->phys;
1709 input->id.bustype = BUS_HOST;
1710 input->id.product = 0x06;
1711 input->dev.parent = &device->dev;
1712 input->evbit[0] = BIT(EV_KEY);
1713 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1714 set_bit(KEY_VIDEO_NEXT, input->keybit);
1715 set_bit(KEY_VIDEO_PREV, input->keybit);
1716 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1717 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1718 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1719 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1720 set_bit(KEY_DISPLAY_OFF, input->keybit);
1722 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
1723 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1724 video->flags.multihead ? "yes" : "no",
1725 video->flags.rom ? "yes" : "no",
1726 video->flags.post ? "yes" : "no");
1728 video->pm_nb.notifier_call = acpi_video_resume;
1729 video->pm_nb.priority = 0;
1730 error = register_pm_notifier(&video->pm_nb);
1731 if (error)
1732 goto err_stop_video;
1734 error = input_register_device(input);
1735 if (error)
1736 goto err_unregister_pm_notifier;
1738 return 0;
1740 err_unregister_pm_notifier:
1741 unregister_pm_notifier(&video->pm_nb);
1742 err_stop_video:
1743 acpi_video_bus_stop_devices(video);
1744 err_free_input_dev:
1745 input_free_device(input);
1746 err_put_video:
1747 acpi_video_bus_put_devices(video);
1748 kfree(video->attached_array);
1749 err_free_video:
1750 kfree(video);
1751 device->driver_data = NULL;
1753 return error;
1756 static int acpi_video_bus_remove(struct acpi_device *device)
1758 struct acpi_video_bus *video = NULL;
1761 if (!device || !acpi_driver_data(device))
1762 return -EINVAL;
1764 video = acpi_driver_data(device);
1766 unregister_pm_notifier(&video->pm_nb);
1768 acpi_video_bus_stop_devices(video);
1769 acpi_video_bus_put_devices(video);
1771 input_unregister_device(video->input);
1772 kfree(video->attached_array);
1773 kfree(video);
1775 return 0;
1778 static int __init is_i740(struct pci_dev *dev)
1780 if (dev->device == 0x00D1)
1781 return 1;
1782 if (dev->device == 0x7000)
1783 return 1;
1784 return 0;
1787 static int __init intel_opregion_present(void)
1789 int opregion = 0;
1790 struct pci_dev *dev = NULL;
1791 u32 address;
1793 for_each_pci_dev(dev) {
1794 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
1795 continue;
1796 if (dev->vendor != PCI_VENDOR_ID_INTEL)
1797 continue;
1798 /* We don't want to poke around undefined i740 registers */
1799 if (is_i740(dev))
1800 continue;
1801 pci_read_config_dword(dev, 0xfc, &address);
1802 if (!address)
1803 continue;
1804 opregion = 1;
1806 return opregion;
1809 int acpi_video_register(void)
1811 int result = 0;
1812 if (register_count) {
1814 * if the function of acpi_video_register is already called,
1815 * don't register the acpi_vide_bus again and return no error.
1817 return 0;
1820 result = acpi_bus_register_driver(&acpi_video_bus);
1821 if (result < 0)
1822 return -ENODEV;
1825 * When the acpi_video_bus is loaded successfully, increase
1826 * the counter reference.
1828 register_count = 1;
1830 return 0;
1832 EXPORT_SYMBOL(acpi_video_register);
1834 void acpi_video_unregister(void)
1836 if (!register_count) {
1838 * If the acpi video bus is already unloaded, don't
1839 * unload it again and return directly.
1841 return;
1843 acpi_bus_unregister_driver(&acpi_video_bus);
1845 register_count = 0;
1847 return;
1849 EXPORT_SYMBOL(acpi_video_unregister);
1852 * This is kind of nasty. Hardware using Intel chipsets may require
1853 * the video opregion code to be run first in order to initialise
1854 * state before any ACPI video calls are made. To handle this we defer
1855 * registration of the video class until the opregion code has run.
1858 static int __init acpi_video_init(void)
1861 * Let the module load even if ACPI is disabled (e.g. due to
1862 * a broken BIOS) so that i915.ko can still be loaded on such
1863 * old systems without an AcpiOpRegion.
1865 * acpi_video_register() will report -ENODEV later as well due
1866 * to acpi_disabled when i915.ko tries to register itself afterwards.
1868 if (acpi_disabled)
1869 return 0;
1871 dmi_check_system(video_dmi_table);
1873 if (intel_opregion_present())
1874 return 0;
1876 return acpi_video_register();
1879 static void __exit acpi_video_exit(void)
1881 acpi_video_unregister();
1883 return;
1886 module_init(acpi_video_init);
1887 module_exit(acpi_video_exit);