27 #include <linux/kernel.h>
28 #include <linux/module.h>
30 #include <linux/types.h>
31 #include <linux/list.h>
33 #include <linux/input.h>
37 #include <linux/pci.h>
39 #include <linux/slab.h>
40 #include <asm/uaccess.h>
47 #define PREFIX "ACPI: "
49 #define ACPI_VIDEO_BUS_NAME "Video Bus"
50 #define ACPI_VIDEO_DEVICE_NAME "Video Device"
51 #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
52 #define ACPI_VIDEO_NOTIFY_PROBE 0x81
53 #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
54 #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
55 #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
57 #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
58 #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
59 #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
60 #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
61 #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
63 #define MAX_NAME_LEN 20
65 #define _COMPONENT ACPI_VIDEO_COMPONENT
72 static bool brightness_switch_enabled = 1;
79 static bool allow_duplicates;
86 static bool use_bios_initial_backlight = 1;
89 static int register_count = 0;
90 static int acpi_video_bus_add(
struct acpi_device *
device);
91 static int acpi_video_bus_remove(
struct acpi_device *
device,
int type);
92 static void acpi_video_bus_notify(
struct acpi_device *
device,
u32 event);
103 .ids = video_device_ids,
105 .add = acpi_video_bus_add,
106 .remove = acpi_video_bus_remove,
107 .notify = acpi_video_bus_notify,
209 static const char device_decode[][30] = {
210 "motherboard VGA device",
217 static void acpi_video_device_rebind(
struct acpi_video_bus *video);
220 static int acpi_video_device_enumerate(
struct acpi_video_bus *video);
223 static int acpi_video_device_lcd_get_level_current(
234 unsigned long long cur_level;
239 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, 0))
252 int request_level = bd->
props.brightness + 2;
256 return acpi_video_device_lcd_set_level(vd,
261 .get_brightness = acpi_video_get_brightness,
262 .update_status = acpi_video_set_brightness,
281 unsigned long long level;
284 if (acpi_video_device_lcd_get_level_current(video, &level, 0))
286 for (offset = 2; offset < video->
brightness->count; offset++)
287 if (level == video->
brightness->levels[offset]) {
288 *state = video->
brightness->count - offset - 1;
298 struct acpi_device *device = cooling_dev->
devdata;
307 return acpi_video_device_lcd_set_level(video, level);
311 .get_max_state = video_get_max_state,
312 .get_cur_state = video_get_cur_state,
313 .set_cur_state = video_set_cur_state,
369 for (state = 2; state < device->
brightness->count; state++)
370 if (level == device->
brightness->levels[state]) {
372 device->
backlight->props.brightness = state - 2;
385 static int bqc_offset_aml_bug_workaround;
388 bqc_offset_aml_bug_workaround = 9;
392 static int video_ignore_initial_backlight(
const struct dmi_system_id *
d)
394 use_bios_initial_backlight = 0;
403 .callback = video_set_bqc_offset,
404 .ident =
"Acer Aspire 5720",
411 .callback = video_set_bqc_offset,
412 .ident =
"Acer Aspire 5710Z",
419 .callback = video_set_bqc_offset,
420 .ident =
"eMachines E510",
427 .callback = video_set_bqc_offset,
428 .ident =
"Acer Aspire 5315",
435 .callback = video_set_bqc_offset,
436 .ident =
"Acer Aspire 7720",
443 .callback = video_ignore_initial_backlight,
444 .ident =
"HP Folio 13-2000",
455 unsigned long long *level,
int init)
460 if (device->
cap._BQC || device->
cap._BCQ) {
461 char *
buf = device->
cap._BQC ?
"_BQC" :
"_BCQ";
466 if (device->
brightness->flags._BQC_use_index) {
470 *level = device->
brightness->levels[*level + 2];
473 *level += bqc_offset_aml_bug_workaround;
474 for (i = 2; i < device->
brightness->count; i++)
475 if (device->
brightness->levels[i] == *level) {
485 "%s returned an invalid level",
487 device->
cap._BQC = device->
cap._BCQ = 0;
498 device->
cap._BQC = device->
cap._BCQ = 0;
523 else if (length == 256)
569 acpi_video_bus_DOS(
struct acpi_video_bus *video,
int bios_flag,
int lcd_flag)
575 if (!video->
cap._DOS)
578 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1)
580 arg0.
integer.value = (lcd_flag << 2) | bios_flag;
595 acpi_video_cmp_level(
const void *
a,
const void *
b)
597 return *(
int *)a - *(
int *)
b;
615 unsigned long long level, level_old;
620 if (!
ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
622 "LCD brightness level\n"));
643 for (i = 0; i < obj->
package.count; i++) {
662 for (i = 2; i <
count; i++) {
669 if (level_ac_battery < 2) {
670 level_ac_battery = 2 - level_ac_battery;
671 br->
flags._BCL_no_ac_battery_levels = 1;
672 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
674 count += level_ac_battery;
675 }
else if (level_ac_battery > 2)
679 if (max_level == br->
levels[2]) {
680 br->
flags._BCL_reversed = 1;
682 acpi_video_cmp_level,
NULL);
683 }
else if (max_level != br->
levels[count - 1])
685 "Found unordered _BCL package\n"));
691 if ((max_level < 100) && (max_level <= (count - 2)))
692 br->
flags._BCL_use_index = 1;
698 br->
flags._BCM_use_index = br->
flags._BCL_use_index;
703 if (!device->
cap._BQC)
706 result = acpi_video_device_lcd_get_level_current(device, &level_old, 1);
708 goto out_free_levels;
713 result = acpi_video_device_lcd_set_level(device, max_level);
715 goto out_free_levels;
717 result = acpi_video_device_lcd_get_level_current(device, &level, 0);
719 goto out_free_levels;
721 br->
flags._BQC_use_index = (level == max_level ? 0 : 1);
723 if (!br->
flags._BQC_use_index) {
730 if (use_bios_initial_backlight) {
731 for (i = 2; i < br->
count; i++)
732 if (level_old == br->
levels[i])
738 if (br->
flags._BCL_reversed)
739 level_old = (br->
count - 1) - level_old;
740 level = br->
levels[level_old];
743 result = acpi_video_device_lcd_set_level(device, level);
745 goto out_free_levels;
748 "found %d brightness levels\n", count - 2));
778 device->
cap._ADR = 1;
781 device->
cap._BCL = 1;
784 device->
cap._BCM = 1;
787 device->
cap._BQC = 1;
791 device->
cap._BCQ = 1;
795 device->
cap._DDC = 1;
804 static int count = 0;
807 result = acpi_video_init_brightness(device);
825 props.max_brightness = device->
brightness->count - 3;
840 acpi_video_get_brightness(device->
backlight);
843 device->
dev, &video_cooling_ops);
857 dev_info(&device->
dev->dev,
"registered as cooling_device%d\n",
865 &device->
dev->dev.kobj,
"device");
929 if (video->
cap._DOS || video->
cap._DOD) {
930 if (!video->
cap._DOS) {
932 "ACPI(%s) defines _DOD but not _DOS\n",
933 acpi_device_bid(video->
device));
935 video->
flags.multihead = 1;
940 if (video->
cap._ROM) {
941 video->
flags.rom = 1;
946 if (video->
cap._GPD && video->
cap._SPD && video->
cap._VPO) {
947 video->
flags.post = 1;
976 unsigned long device_id)
991 acpi_video_bus_get_one_device(
struct acpi_device *device,
999 if (!device || !video)
1012 device->driver_data =
data;
1015 data->
video = video;
1018 attribute = acpi_video_get_device_attr(video, device_id);
1023 data->
flags.crt = 1;
1026 data->
flags.tvout = 1;
1029 data->
flags.dvi = 1;
1032 data->
flags.lcd = 1;
1035 data->
flags.unknown = 1;
1039 data->
flags.bios = 1;
1042 device_type = acpi_video_get_device_type(video,
1045 switch (device_type & 0xffe2ffff) {
1047 data->
flags.crt = 1;
1050 data->
flags.lcd = 1;
1053 data->
flags.tvout = 1;
1056 data->
flags.unknown = 1;
1060 acpi_video_device_bind(video, data);
1061 acpi_video_device_find_cap(data);
1065 acpi_video_device_notify,
1069 "Error installing notify handler\n");
1099 static void acpi_video_device_rebind(
struct acpi_video_bus *video)
1106 acpi_video_device_bind(video, dev);
1131 for (i = 0; i < video->attached_count; i++) {
1132 ids = &video->attached_array[
i];
1133 if (device->device_id == (ids->
value.
int_val & 0xffff)) {
1151 static int acpi_video_device_enumerate(
struct acpi_video_bus *video)
1177 active_list = kcalloc(1 + dod->
package.count,
1186 for (i = 0; i < dod->
package.count; i++) {
1191 "Invalid _DOD data in element %d\n", i);
1217 max = max_below = 0;
1218 min = min_above = 255;
1220 for (i = 2; i < device->
brightness->count; i++) {
1222 if (
abs(l - level_current) <
abs(delta)) {
1223 delta = l - level_current;
1229 level_current +=
delta;
1230 for (i = 2; i < device->
brightness->count; i++) {
1236 if (l < min_above && l > level_current)
1238 if (l > max_below && l < level_current)
1244 return (level_current < max) ? min_above :
min;
1246 return (level_current < max) ? min_above :
max;
1248 return (level_current > min) ? max_below :
min;
1253 return level_current;
1260 unsigned long long level_current, level_next;
1270 result = acpi_video_device_lcd_get_level_current(device,
1275 level_next = acpi_video_get_next_level(device, level_current, event);
1277 result = acpi_video_device_lcd_set_level(device, level_next);
1299 if (!device || !acpi_driver_data(device))
1302 video = acpi_driver_data(device);
1311 if (!video_device->
cap._DDC)
1317 if (!video_device->
flags.crt)
1321 if (!video_device->
flags.tvout)
1325 if (!video_device->
flags.dvi)
1329 if (!video_device->
flags.lcd)
1333 }
else if (video_device->
device_id != device_id) {
1337 status = acpi_video_device_EDID(video_device, &buffer, length);
1342 status = acpi_video_device_EDID(video_device, &buffer,
1350 *edid = buffer->
buffer.pointer;
1360 struct acpi_device *device)
1363 struct acpi_device *
dev;
1370 acpi_video_device_enumerate(video);
1374 status = acpi_video_bus_get_one_device(dev, video);
1377 "Can't attach device\n");
1388 if (!device || !device->
video)
1393 acpi_video_device_notify);
1396 "Can't remove video notify handler\n");
1414 static int acpi_video_bus_put_devices(
struct acpi_video_bus *video)
1423 status = acpi_video_bus_put_one_device(dev);
1426 "hhuuhhuu bug in acpi video driver.\n");
1443 static int acpi_video_bus_start_devices(
struct acpi_video_bus *video)
1445 return acpi_video_bus_DOS(video, 0, 0);
1448 static int acpi_video_bus_stop_devices(
struct acpi_video_bus *video)
1450 return acpi_video_bus_DOS(video, 0, 1);
1453 static void acpi_video_bus_notify(
struct acpi_device *device,
u32 event)
1456 struct input_dev *
input;
1462 input = video->
input;
1467 acpi_bus_generate_proc_event(device, event, 0);
1473 acpi_video_device_enumerate(video);
1474 acpi_video_device_rebind(video);
1475 acpi_bus_generate_proc_event(device, event, 0);
1480 acpi_bus_generate_proc_event(device, event, 0);
1484 acpi_bus_generate_proc_event(device, event, 0);
1488 acpi_bus_generate_proc_event(device, event, 0);
1494 "Unsupported event [0x%x]\n", event));
1503 input_report_key(input, keycode, 1);
1505 input_report_key(input, keycode, 0);
1515 struct acpi_device *device =
NULL;
1517 struct input_dev *
input;
1523 device = video_device->
dev;
1524 bus = video_device->
video;
1529 if (brightness_switch_enabled)
1530 acpi_video_switch_brightness(video_device, event);
1531 acpi_bus_generate_proc_event(device, event, 0);
1535 if (brightness_switch_enabled)
1536 acpi_video_switch_brightness(video_device, event);
1537 acpi_bus_generate_proc_event(device, event, 0);
1541 if (brightness_switch_enabled)
1542 acpi_video_switch_brightness(video_device, event);
1543 acpi_bus_generate_proc_event(device, event, 0);
1547 if (brightness_switch_enabled)
1548 acpi_video_switch_brightness(video_device, event);
1549 acpi_bus_generate_proc_event(device, event, 0);
1553 if (brightness_switch_enabled)
1554 acpi_video_switch_brightness(video_device, event);
1555 acpi_bus_generate_proc_event(device, event, 0);
1560 "Unsupported event [0x%x]\n", event));
1567 input_report_key(input, keycode, 1);
1569 input_report_key(input, keycode, 0);
1577 unsigned long val,
void *ign)
1596 if (video_device && video_device->
backlight)
1597 acpi_video_set_brightness(video_device->
backlight);
1607 struct acpi_device *device =
context;
1608 struct acpi_device *sibling;
1611 if (handle == device->handle)
1624 static int instance;
1626 static int acpi_video_bus_add(
struct acpi_device *device)
1629 struct input_dev *
input;
1634 device->parent->handle, 1,
1635 acpi_video_bus_match,
NULL,
1639 "Duplicate ACPI video bus devices for the"
1640 " same VGA controller, please try module "
1641 "parameter \"video.allow_duplicates=1\""
1642 "if the current driver doesn't work.\n");
1643 if (!allow_duplicates)
1652 if (!
strcmp(device->pnp.bus_id,
"VID")) {
1654 device->pnp.bus_id[3] =
'0' + instance;
1658 if (!
strcmp(device->pnp.bus_id,
"VGA")) {
1660 device->pnp.bus_id[3] =
'0' + instance;
1667 device->driver_data = video;
1669 acpi_video_bus_find_cap(video);
1670 error = acpi_video_bus_check(video);
1672 goto err_free_video;
1677 error = acpi_video_bus_get_devices(video, device);
1679 goto err_free_video;
1681 video->
input = input = input_allocate_device();
1687 error = acpi_video_bus_start_devices(video);
1689 goto err_free_input_dev;
1694 input->name = acpi_device_name(video->
device);
1695 input->phys = video->
phys;
1697 input->id.product = 0x06;
1698 input->dev.parent = &device->dev;
1711 video->
flags.multihead ?
"yes" :
"no",
1712 video->
flags.rom ?
"yes" :
"no",
1713 video->
flags.post ?
"yes" :
"no");
1715 video->
pm_nb.notifier_call = acpi_video_resume;
1716 video->
pm_nb.priority = 0;
1717 error = register_pm_notifier(&video->
pm_nb);
1719 goto err_stop_video;
1721 error = input_register_device(input);
1723 goto err_unregister_pm_notifier;
1727 err_unregister_pm_notifier:
1728 unregister_pm_notifier(&video->
pm_nb);
1730 acpi_video_bus_stop_devices(video);
1732 input_free_device(input);
1734 acpi_video_bus_put_devices(video);
1738 device->driver_data =
NULL;
1743 static int acpi_video_bus_remove(
struct acpi_device *device,
int type)
1748 if (!device || !acpi_driver_data(device))
1751 video = acpi_driver_data(device);
1753 unregister_pm_notifier(&video->
pm_nb);
1755 acpi_video_bus_stop_devices(video);
1756 acpi_video_bus_put_devices(video);
1758 input_unregister_device(video->
input);
1767 if (dev->
device == 0x00D1)
1769 if (dev->
device == 0x7000)
1774 static int __init intel_opregion_present(
void)
1788 pci_read_config_dword(dev, 0xfc, &address);
1799 if (register_count) {
1823 if (!register_count) {
1845 static int __init acpi_video_init(
void)
1849 if (intel_opregion_present())
1855 static void __exit acpi_video_exit(
void)