2 * video.c - ACPI Video Driver ($Revision:$)
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/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/input.h>
36 #include <linux/backlight.h>
37 #include <linux/thermal.h>
38 #include <linux/video_output.h>
39 #include <linux/sort.h>
40 #include <linux/pci.h>
41 #include <linux/pci_ids.h>
42 #include <asm/uaccess.h>
43 #include <linux/dmi.h>
44 #include <acpi/acpi_bus.h>
45 #include <acpi/acpi_drivers.h>
47 #define PREFIX "ACPI: "
49 #define ACPI_VIDEO_CLASS "video"
50 #define ACPI_VIDEO_BUS_NAME "Video Bus"
51 #define ACPI_VIDEO_DEVICE_NAME "Video Device"
52 #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
53 #define ACPI_VIDEO_NOTIFY_PROBE 0x81
54 #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
55 #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
56 #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
58 #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
59 #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
60 #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
61 #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
62 #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
64 #define MAX_NAME_LEN 20
66 #define ACPI_VIDEO_DISPLAY_CRT 1
67 #define ACPI_VIDEO_DISPLAY_TV 2
68 #define ACPI_VIDEO_DISPLAY_DVI 3
69 #define ACPI_VIDEO_DISPLAY_LCD 4
71 #define _COMPONENT ACPI_VIDEO_COMPONENT
72 ACPI_MODULE_NAME("video");
74 MODULE_AUTHOR("Bruno Ducrot");
75 MODULE_DESCRIPTION("ACPI Video Driver");
76 MODULE_LICENSE("GPL");
78 static int brightness_switch_enabled = 1;
79 module_param(brightness_switch_enabled, bool, 0644);
82 * By default, we don't allow duplicate ACPI video bus devices
83 * under the same VGA controller
85 static int allow_duplicates;
86 module_param(allow_duplicates, bool, 0644);
88 static int register_count = 0;
89 static int acpi_video_bus_add(struct acpi_device *device);
90 static int acpi_video_bus_remove(struct acpi_device *device, int type);
91 static int acpi_video_resume(struct acpi_device *device);
92 static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
94 static const struct acpi_device_id video_device_ids[] = {
98 MODULE_DEVICE_TABLE(acpi, video_device_ids);
100 static struct acpi_driver acpi_video_bus = {
102 .class = ACPI_VIDEO_CLASS,
103 .ids = video_device_ids,
105 .add = acpi_video_bus_add,
106 .remove = acpi_video_bus_remove,
107 .resume = acpi_video_resume,
108 .notify = acpi_video_bus_notify,
112 struct acpi_video_bus_flags {
113 u8 multihead:1; /* can switch video heads */
114 u8 rom:1; /* can retrieve a video rom */
115 u8 post:1; /* can configure the head to */
119 struct acpi_video_bus_cap {
120 u8 _DOS:1; /*Enable/Disable output switching */
121 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
122 u8 _ROM:1; /*Get ROM Data */
123 u8 _GPD:1; /*Get POST Device */
124 u8 _SPD:1; /*Set POST Device */
125 u8 _VPO:1; /*Video POST Options */
129 struct acpi_video_device_attrib {
130 u32 display_index:4; /* A zero-based instance of the Display */
131 u32 display_port_attachment:4; /*This field differentiates the display type */
132 u32 display_type:4; /*Describe the specific type in use */
133 u32 vendor_specific:4; /*Chipset Vendor Specific */
134 u32 bios_can_detect:1; /*BIOS can detect the device */
135 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
137 u32 pipe_id:3; /*For VGA multiple-head devices. */
138 u32 reserved:10; /*Must be 0 */
139 u32 device_id_scheme:1; /*Device ID Scheme */
142 struct acpi_video_enumerated_device {
145 struct acpi_video_device_attrib attrib;
147 struct acpi_video_device *bind_info;
150 struct acpi_video_bus {
151 struct acpi_device *device;
153 struct acpi_video_enumerated_device *attached_array;
155 struct acpi_video_bus_cap cap;
156 struct acpi_video_bus_flags flags;
157 struct list_head video_device_list;
158 struct mutex device_list_lock; /* protects video_device_list */
159 struct proc_dir_entry *dir;
160 struct input_dev *input;
161 char phys[32]; /* for input device */
164 struct acpi_video_device_flags {
174 struct acpi_video_device_cap {
175 u8 _ADR:1; /*Return the unique ID */
176 u8 _BCL:1; /*Query list of brightness control levels supported */
177 u8 _BCM:1; /*Set the brightness level */
178 u8 _BQC:1; /* Get current brightness level */
179 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
180 u8 _DDC:1; /*Return the EDID for this device */
181 u8 _DCS:1; /*Return status of output device */
182 u8 _DGS:1; /*Query graphics state */
183 u8 _DSS:1; /*Device state set */
186 struct acpi_video_brightness_flags {
187 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
188 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
189 u8 _BCL_use_index:1; /* levels in _BCL are index values */
190 u8 _BCM_use_index:1; /* input of _BCM is an index value */
191 u8 _BQC_use_index:1; /* _BQC returns an index value */
194 struct acpi_video_device_brightness {
198 struct acpi_video_brightness_flags flags;
201 struct acpi_video_device {
202 unsigned long device_id;
203 struct acpi_video_device_flags flags;
204 struct acpi_video_device_cap cap;
205 struct list_head entry;
206 struct acpi_video_bus *video;
207 struct acpi_device *dev;
208 struct acpi_video_device_brightness *brightness;
209 struct backlight_device *backlight;
210 struct thermal_cooling_device *cooling_dev;
211 struct output_device *output_dev;
215 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
216 static const struct file_operations acpi_video_bus_info_fops = {
217 .owner = THIS_MODULE,
218 .open = acpi_video_bus_info_open_fs,
221 .release = single_release,
224 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
225 static const struct file_operations acpi_video_bus_ROM_fops = {
226 .owner = THIS_MODULE,
227 .open = acpi_video_bus_ROM_open_fs,
230 .release = single_release,
233 static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
235 static const struct file_operations acpi_video_bus_POST_info_fops = {
236 .owner = THIS_MODULE,
237 .open = acpi_video_bus_POST_info_open_fs,
240 .release = single_release,
243 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
244 static ssize_t acpi_video_bus_write_POST(struct file *file,
245 const char __user *buffer, size_t count, loff_t *data);
246 static const struct file_operations acpi_video_bus_POST_fops = {
247 .owner = THIS_MODULE,
248 .open = acpi_video_bus_POST_open_fs,
250 .write = acpi_video_bus_write_POST,
252 .release = single_release,
255 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
256 static ssize_t acpi_video_bus_write_DOS(struct file *file,
257 const char __user *buffer, size_t count, loff_t *data);
258 static const struct file_operations acpi_video_bus_DOS_fops = {
259 .owner = THIS_MODULE,
260 .open = acpi_video_bus_DOS_open_fs,
262 .write = acpi_video_bus_write_DOS,
264 .release = single_release,
268 static int acpi_video_device_info_open_fs(struct inode *inode,
270 static const struct file_operations acpi_video_device_info_fops = {
271 .owner = THIS_MODULE,
272 .open = acpi_video_device_info_open_fs,
275 .release = single_release,
278 static int acpi_video_device_state_open_fs(struct inode *inode,
280 static ssize_t acpi_video_device_write_state(struct file *file,
281 const char __user *buffer, size_t count, loff_t *data);
282 static const struct file_operations acpi_video_device_state_fops = {
283 .owner = THIS_MODULE,
284 .open = acpi_video_device_state_open_fs,
286 .write = acpi_video_device_write_state,
288 .release = single_release,
291 static int acpi_video_device_brightness_open_fs(struct inode *inode,
293 static ssize_t acpi_video_device_write_brightness(struct file *file,
294 const char __user *buffer, size_t count, loff_t *data);
295 static const struct file_operations acpi_video_device_brightness_fops = {
296 .owner = THIS_MODULE,
297 .open = acpi_video_device_brightness_open_fs,
299 .write = acpi_video_device_write_brightness,
301 .release = single_release,
304 static int acpi_video_device_EDID_open_fs(struct inode *inode,
306 static const struct file_operations acpi_video_device_EDID_fops = {
307 .owner = THIS_MODULE,
308 .open = acpi_video_device_EDID_open_fs,
311 .release = single_release,
314 static const char device_decode[][30] = {
315 "motherboard VGA device",
321 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
322 static void acpi_video_device_rebind(struct acpi_video_bus *video);
323 static void acpi_video_device_bind(struct acpi_video_bus *video,
324 struct acpi_video_device *device);
325 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
326 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
328 static int acpi_video_device_lcd_get_level_current(
329 struct acpi_video_device *device,
330 unsigned long long *level, int init);
331 static int acpi_video_get_next_level(struct acpi_video_device *device,
332 u32 level_current, u32 event);
333 static int acpi_video_switch_brightness(struct acpi_video_device *device,
335 static int acpi_video_device_get_state(struct acpi_video_device *device,
336 unsigned long long *state);
337 static int acpi_video_output_get(struct output_device *od);
338 static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
340 /*backlight device sysfs support*/
341 static int acpi_video_get_brightness(struct backlight_device *bd)
343 unsigned long long cur_level;
345 struct acpi_video_device *vd =
346 (struct acpi_video_device *)bl_get_data(bd);
348 if (acpi_video_device_lcd_get_level_current(vd, &cur_level, 0))
350 for (i = 2; i < vd->brightness->count; i++) {
351 if (vd->brightness->levels[i] == cur_level)
352 /* The first two entries are special - see page 575
353 of the ACPI spec 3.0 */
359 static int acpi_video_set_brightness(struct backlight_device *bd)
361 int request_level = bd->props.brightness + 2;
362 struct acpi_video_device *vd =
363 (struct acpi_video_device *)bl_get_data(bd);
365 return acpi_video_device_lcd_set_level(vd,
366 vd->brightness->levels[request_level]);
369 static struct backlight_ops acpi_backlight_ops = {
370 .get_brightness = acpi_video_get_brightness,
371 .update_status = acpi_video_set_brightness,
374 /*video output device sysfs support*/
375 static int acpi_video_output_get(struct output_device *od)
377 unsigned long long state;
378 struct acpi_video_device *vd =
379 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
380 acpi_video_device_get_state(vd, &state);
384 static int acpi_video_output_set(struct output_device *od)
386 unsigned long state = od->request_state;
387 struct acpi_video_device *vd=
388 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
389 return acpi_video_device_set_state(vd, state);
392 static struct output_properties acpi_output_properties = {
393 .set_state = acpi_video_output_set,
394 .get_status = acpi_video_output_get,
398 /* thermal cooling device callbacks */
399 static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
402 struct acpi_device *device = cooling_dev->devdata;
403 struct acpi_video_device *video = acpi_driver_data(device);
405 *state = video->brightness->count - 3;
409 static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
412 struct acpi_device *device = cooling_dev->devdata;
413 struct acpi_video_device *video = acpi_driver_data(device);
414 unsigned long long level;
417 if (acpi_video_device_lcd_get_level_current(video, &level, 0))
419 for (offset = 2; offset < video->brightness->count; offset++)
420 if (level == video->brightness->levels[offset]) {
421 *state = video->brightness->count - offset - 1;
429 video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
431 struct acpi_device *device = cooling_dev->devdata;
432 struct acpi_video_device *video = acpi_driver_data(device);
435 if ( state >= video->brightness->count - 2)
438 state = video->brightness->count - state;
439 level = video->brightness->levels[state -1];
440 return acpi_video_device_lcd_set_level(video, level);
443 static struct thermal_cooling_device_ops video_cooling_ops = {
444 .get_max_state = video_get_max_state,
445 .get_cur_state = video_get_cur_state,
446 .set_cur_state = video_set_cur_state,
449 /* --------------------------------------------------------------------------
451 -------------------------------------------------------------------------- */
456 acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
460 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
466 acpi_video_device_get_state(struct acpi_video_device *device,
467 unsigned long long *state)
471 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
477 acpi_video_device_set_state(struct acpi_video_device *device, int state)
480 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
481 struct acpi_object_list args = { 1, &arg0 };
482 unsigned long long ret;
485 arg0.integer.value = state;
486 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
492 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
493 union acpi_object **levels)
496 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
497 union acpi_object *obj;
502 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
503 if (!ACPI_SUCCESS(status))
505 obj = (union acpi_object *)buffer.pointer;
506 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
507 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
517 kfree(buffer.pointer);
523 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
526 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
527 struct acpi_object_list args = { 1, &arg0 };
530 arg0.integer.value = level;
532 status = acpi_evaluate_object(device->dev->handle, "_BCM",
534 if (ACPI_FAILURE(status)) {
535 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
539 device->brightness->curr = level;
540 for (state = 2; state < device->brightness->count; state++)
541 if (level == device->brightness->levels[state]) {
542 if (device->backlight)
543 device->backlight->props.brightness = state - 2;
547 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
552 * For some buggy _BQC methods, we need to add a constant value to
553 * the _BQC return value to get the actual current brightness level
556 static int bqc_offset_aml_bug_workaround;
557 static int __init video_set_bqc_offset(const struct dmi_system_id *d)
559 bqc_offset_aml_bug_workaround = 9;
563 static struct dmi_system_id video_dmi_table[] __initdata = {
565 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
568 .callback = video_set_bqc_offset,
569 .ident = "Acer Aspire 5720",
571 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
572 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
576 .callback = video_set_bqc_offset,
577 .ident = "Acer Aspire 5710Z",
579 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
580 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
584 .callback = video_set_bqc_offset,
585 .ident = "eMachines E510",
587 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
588 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
592 .callback = video_set_bqc_offset,
593 .ident = "Acer Aspire 5315",
595 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
596 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
600 .callback = video_set_bqc_offset,
601 .ident = "Acer Aspire 7720",
603 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
604 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
611 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
612 unsigned long long *level, int init)
614 acpi_status status = AE_OK;
617 if (device->cap._BQC || device->cap._BCQ) {
618 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
620 status = acpi_evaluate_integer(device->dev->handle, buf,
622 if (ACPI_SUCCESS(status)) {
623 if (device->brightness->flags._BQC_use_index) {
624 if (device->brightness->flags._BCL_reversed)
625 *level = device->brightness->count
627 *level = device->brightness->levels[*level + 2];
630 *level += bqc_offset_aml_bug_workaround;
631 for (i = 2; i < device->brightness->count; i++)
632 if (device->brightness->levels[i] == *level) {
633 device->brightness->curr = *level;
638 * BQC returned an invalid level.
641 ACPI_WARNING((AE_INFO,
642 "%s returned an invalid level",
644 device->cap._BQC = device->cap._BCQ = 0;
648 * should we return an error or ignore this failure?
649 * dev->brightness->curr is a cached value which stores
650 * the correct current backlight level in most cases.
651 * ACPI video backlight still works w/ buggy _BQC.
652 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
654 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
655 device->cap._BQC = device->cap._BCQ = 0;
659 *level = device->brightness->curr;
664 acpi_video_device_EDID(struct acpi_video_device *device,
665 union acpi_object **edid, ssize_t length)
668 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
669 union acpi_object *obj;
670 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
671 struct acpi_object_list args = { 1, &arg0 };
679 arg0.integer.value = 1;
680 else if (length == 256)
681 arg0.integer.value = 2;
685 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
686 if (ACPI_FAILURE(status))
689 obj = buffer.pointer;
691 if (obj && obj->type == ACPI_TYPE_BUFFER)
694 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
705 acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
708 unsigned long long tmp;
709 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
710 struct acpi_object_list args = { 1, &arg0 };
713 arg0.integer.value = option;
715 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
716 if (ACPI_SUCCESS(status))
717 status = tmp ? (-EINVAL) : (AE_OK);
723 acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
727 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
733 acpi_video_bus_POST_options(struct acpi_video_bus *video,
734 unsigned long long *options)
738 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
746 * video : video bus device pointer
748 * 0. The system BIOS should NOT automatically switch(toggle)
749 * the active display output.
750 * 1. The system BIOS should automatically switch (toggle) the
751 * active display output. No switch event.
752 * 2. The _DGS value should be locked.
753 * 3. The system BIOS should not automatically switch (toggle) the
754 * active display output, but instead generate the display switch
757 * 0. The system BIOS should automatically control the brightness level
758 * of the LCD when the power changes from AC to DC
759 * 1. The system BIOS should NOT automatically control the brightness
760 * level of the LCD when the power changes from AC to DC.
766 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
769 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
770 struct acpi_object_list args = { 1, &arg0 };
773 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
777 arg0.integer.value = (lcd_flag << 2) | bios_flag;
778 video->dos_setting = arg0.integer.value;
779 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
786 * Simple comparison function used to sort backlight levels.
790 acpi_video_cmp_level(const void *a, const void *b)
792 return *(int *)a - *(int *)b;
797 * device : video output device (LCD, CRT, ..)
800 * Maximum brightness level
802 * Allocate and initialize device->brightness.
806 acpi_video_init_brightness(struct acpi_video_device *device)
808 union acpi_object *obj = NULL;
809 int i, max_level = 0, count = 0, level_ac_battery = 0;
810 unsigned long long level, level_old;
811 union acpi_object *o;
812 struct acpi_video_device_brightness *br = NULL;
813 int result = -EINVAL;
815 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
816 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
817 "LCD brightness level\n"));
821 if (obj->package.count < 2)
824 br = kzalloc(sizeof(*br), GFP_KERNEL);
826 printk(KERN_ERR "can't allocate memory\n");
831 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
838 for (i = 0; i < obj->package.count; i++) {
839 o = (union acpi_object *)&obj->package.elements[i];
840 if (o->type != ACPI_TYPE_INTEGER) {
841 printk(KERN_ERR PREFIX "Invalid data\n");
844 br->levels[count] = (u32) o->integer.value;
846 if (br->levels[count] > max_level)
847 max_level = br->levels[count];
852 * some buggy BIOS don't export the levels
853 * when machine is on AC/Battery in _BCL package.
854 * In this case, the first two elements in _BCL packages
855 * are also supported brightness levels that OS should take care of.
857 for (i = 2; i < count; i++) {
858 if (br->levels[i] == br->levels[0])
860 if (br->levels[i] == br->levels[1])
864 if (level_ac_battery < 2) {
865 level_ac_battery = 2 - level_ac_battery;
866 br->flags._BCL_no_ac_battery_levels = 1;
867 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
868 br->levels[i] = br->levels[i - level_ac_battery];
869 count += level_ac_battery;
870 } else if (level_ac_battery > 2)
871 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
873 /* Check if the _BCL package is in a reversed order */
874 if (max_level == br->levels[2]) {
875 br->flags._BCL_reversed = 1;
876 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
877 acpi_video_cmp_level, NULL);
878 } else if (max_level != br->levels[count - 1])
880 "Found unordered _BCL package\n"));
883 device->brightness = br;
885 /* Check the input/output of _BQC/_BCL/_BCM */
886 if ((max_level < 100) && (max_level <= (count - 2)))
887 br->flags._BCL_use_index = 1;
890 * _BCM is always consistent with _BCL,
891 * at least for all the laptops we have ever seen.
893 br->flags._BCM_use_index = br->flags._BCL_use_index;
895 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
896 br->curr = level = max_level;
898 if (!device->cap._BQC)
901 result = acpi_video_device_lcd_get_level_current(device, &level_old, 1);
903 goto out_free_levels;
906 * Set the level to maximum and check if _BQC uses indexed value
908 result = acpi_video_device_lcd_set_level(device, max_level);
910 goto out_free_levels;
912 result = acpi_video_device_lcd_get_level_current(device, &level, 0);
914 goto out_free_levels;
916 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
918 if (!br->flags._BQC_use_index) {
920 * Set the backlight to the initial state.
921 * On some buggy laptops, _BQC returns an uninitialized value
922 * when invoked for the first time, i.e. level_old is invalid.
923 * set the backlight to max_level in this case
925 for (i = 2; i < br->count; i++)
926 if (level_old == br->levels[i])
931 if (br->flags._BCL_reversed)
932 level_old = (br->count - 1) - level_old;
933 level = br->levels[level_old];
936 result = acpi_video_device_lcd_set_level(device, level);
938 goto out_free_levels;
940 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
941 "found %d brightness levels\n", count - 2));
950 device->brightness = NULL;
957 * device : video output device (LCD, CRT, ..)
962 * Find out all required AML methods defined under the output
966 static void acpi_video_device_find_cap(struct acpi_video_device *device)
968 acpi_handle h_dummy1;
970 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
971 device->cap._ADR = 1;
973 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
974 device->cap._BCL = 1;
976 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
977 device->cap._BCM = 1;
979 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
980 device->cap._BQC = 1;
981 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
983 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
984 device->cap._BCQ = 1;
987 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
988 device->cap._DDC = 1;
990 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
991 device->cap._DCS = 1;
993 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
994 device->cap._DGS = 1;
996 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
997 device->cap._DSS = 1;
1000 if (acpi_video_backlight_support()) {
1002 static int count = 0;
1005 result = acpi_video_init_brightness(device);
1008 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
1012 sprintf(name, "acpi_video%d", count++);
1013 device->backlight = backlight_device_register(name,
1014 NULL, device, &acpi_backlight_ops);
1016 if (IS_ERR(device->backlight))
1018 device->backlight->props.max_brightness = device->brightness->count-3;
1020 result = sysfs_create_link(&device->backlight->dev.kobj,
1021 &device->dev->dev.kobj, "device");
1023 printk(KERN_ERR PREFIX "Create sysfs link\n");
1025 device->cooling_dev = thermal_cooling_device_register("LCD",
1026 device->dev, &video_cooling_ops);
1027 if (IS_ERR(device->cooling_dev)) {
1029 * Set cooling_dev to NULL so we don't crash trying to
1031 * Also, why the hell we are returning early and
1032 * not attempt to register video output if cooling
1033 * device registration failed?
1036 device->cooling_dev = NULL;
1040 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1041 device->cooling_dev->id);
1042 result = sysfs_create_link(&device->dev->dev.kobj,
1043 &device->cooling_dev->device.kobj,
1046 printk(KERN_ERR PREFIX "Create sysfs link\n");
1047 result = sysfs_create_link(&device->cooling_dev->device.kobj,
1048 &device->dev->dev.kobj, "device");
1050 printk(KERN_ERR PREFIX "Create sysfs link\n");
1054 if (acpi_video_display_switch_support()) {
1056 if (device->cap._DCS && device->cap._DSS) {
1059 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
1062 sprintf(name, "acpi_video%d", count++);
1063 device->output_dev = video_output_register(name,
1064 NULL, device, &acpi_output_properties);
1072 * device : video output device (VGA)
1077 * Find out all required AML methods defined under the video bus device.
1080 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
1082 acpi_handle h_dummy1;
1084 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
1085 video->cap._DOS = 1;
1087 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
1088 video->cap._DOD = 1;
1090 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
1091 video->cap._ROM = 1;
1093 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
1094 video->cap._GPD = 1;
1096 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
1097 video->cap._SPD = 1;
1099 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
1100 video->cap._VPO = 1;
1105 * Check whether the video bus device has required AML method to
1106 * support the desired features
1109 static int acpi_video_bus_check(struct acpi_video_bus *video)
1111 acpi_status status = -ENOENT;
1112 struct pci_dev *dev;
1117 dev = acpi_get_pci_dev(video->device->handle);
1122 /* Since there is no HID, CID and so on for VGA driver, we have
1123 * to check well known required nodes.
1126 /* Does this device support video switching? */
1127 if (video->cap._DOS || video->cap._DOD) {
1128 if (!video->cap._DOS) {
1129 printk(KERN_WARNING FW_BUG
1130 "ACPI(%s) defines _DOD but not _DOS\n",
1131 acpi_device_bid(video->device));
1133 video->flags.multihead = 1;
1137 /* Does this device support retrieving a video ROM? */
1138 if (video->cap._ROM) {
1139 video->flags.rom = 1;
1143 /* Does this device support configuring which video device to POST? */
1144 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
1145 video->flags.post = 1;
1152 /* --------------------------------------------------------------------------
1153 FS Interface (/proc)
1154 -------------------------------------------------------------------------- */
1156 static struct proc_dir_entry *acpi_video_dir;
1160 static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
1162 struct acpi_video_device *dev = seq->private;
1168 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1169 seq_printf(seq, "type: ");
1171 seq_printf(seq, "CRT\n");
1172 else if (dev->flags.lcd)
1173 seq_printf(seq, "LCD\n");
1174 else if (dev->flags.tvout)
1175 seq_printf(seq, "TVOUT\n");
1176 else if (dev->flags.dvi)
1177 seq_printf(seq, "DVI\n");
1179 seq_printf(seq, "UNKNOWN\n");
1181 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
1188 acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
1190 return single_open(file, acpi_video_device_info_seq_show,
1194 static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
1197 struct acpi_video_device *dev = seq->private;
1198 unsigned long long state;
1204 status = acpi_video_device_get_state(dev, &state);
1205 seq_printf(seq, "state: ");
1206 if (ACPI_SUCCESS(status))
1207 seq_printf(seq, "0x%02llx\n", state);
1209 seq_printf(seq, "<not supported>\n");
1211 status = acpi_video_device_query(dev, &state);
1212 seq_printf(seq, "query: ");
1213 if (ACPI_SUCCESS(status))
1214 seq_printf(seq, "0x%02llx\n", state);
1216 seq_printf(seq, "<not supported>\n");
1223 acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
1225 return single_open(file, acpi_video_device_state_seq_show,
1230 acpi_video_device_write_state(struct file *file,
1231 const char __user * buffer,
1232 size_t count, loff_t * data)
1235 struct seq_file *m = file->private_data;
1236 struct acpi_video_device *dev = m->private;
1237 char str[12] = { 0 };
1241 if (!dev || count >= sizeof(str))
1244 if (copy_from_user(str, buffer, count))
1248 state = simple_strtoul(str, NULL, 0);
1249 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
1251 status = acpi_video_device_set_state(dev, state);
1260 acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
1262 struct acpi_video_device *dev = seq->private;
1266 if (!dev || !dev->brightness) {
1267 seq_printf(seq, "<not supported>\n");
1271 seq_printf(seq, "levels: ");
1272 for (i = 2; i < dev->brightness->count; i++)
1273 seq_printf(seq, " %d", dev->brightness->levels[i]);
1274 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1280 acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
1282 return single_open(file, acpi_video_device_brightness_seq_show,
1287 acpi_video_device_write_brightness(struct file *file,
1288 const char __user * buffer,
1289 size_t count, loff_t * data)
1291 struct seq_file *m = file->private_data;
1292 struct acpi_video_device *dev = m->private;
1293 char str[5] = { 0 };
1294 unsigned int level = 0;
1298 if (!dev || !dev->brightness || count >= sizeof(str))
1301 if (copy_from_user(str, buffer, count))
1305 level = simple_strtoul(str, NULL, 0);
1310 /* validate through the list of available levels */
1311 for (i = 2; i < dev->brightness->count; i++)
1312 if (level == dev->brightness->levels[i]) {
1313 if (!acpi_video_device_lcd_set_level(dev, level))
1321 static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
1323 struct acpi_video_device *dev = seq->private;
1326 union acpi_object *edid = NULL;
1332 status = acpi_video_device_EDID(dev, &edid, 128);
1333 if (ACPI_FAILURE(status)) {
1334 status = acpi_video_device_EDID(dev, &edid, 256);
1337 if (ACPI_FAILURE(status)) {
1341 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1342 for (i = 0; i < edid->buffer.length; i++)
1343 seq_putc(seq, edid->buffer.pointer[i]);
1348 seq_printf(seq, "<not supported>\n");
1356 acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
1358 return single_open(file, acpi_video_device_EDID_seq_show,
1362 static int acpi_video_device_add_fs(struct acpi_device *device)
1364 struct proc_dir_entry *entry, *device_dir;
1365 struct acpi_video_device *vid_dev;
1367 vid_dev = acpi_driver_data(device);
1371 device_dir = proc_mkdir(acpi_device_bid(device),
1372 vid_dev->video->dir);
1377 entry = proc_create_data("info", S_IRUGO, device_dir,
1378 &acpi_video_device_info_fops, acpi_driver_data(device));
1380 goto err_remove_dir;
1383 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
1385 &acpi_video_device_state_fops,
1386 acpi_driver_data(device));
1388 goto err_remove_info;
1390 /* 'brightness' [R/W] */
1391 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
1393 &acpi_video_device_brightness_fops,
1394 acpi_driver_data(device));
1396 goto err_remove_state;
1399 entry = proc_create_data("EDID", S_IRUGO, device_dir,
1400 &acpi_video_device_EDID_fops,
1401 acpi_driver_data(device));
1403 goto err_remove_brightness;
1405 acpi_device_dir(device) = device_dir;
1409 err_remove_brightness:
1410 remove_proc_entry("brightness", device_dir);
1412 remove_proc_entry("state", device_dir);
1414 remove_proc_entry("info", device_dir);
1416 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1420 static int acpi_video_device_remove_fs(struct acpi_device *device)
1422 struct acpi_video_device *vid_dev;
1423 struct proc_dir_entry *device_dir;
1425 vid_dev = acpi_driver_data(device);
1426 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
1429 device_dir = acpi_device_dir(device);
1431 remove_proc_entry("info", device_dir);
1432 remove_proc_entry("state", device_dir);
1433 remove_proc_entry("brightness", device_dir);
1434 remove_proc_entry("EDID", device_dir);
1435 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1436 acpi_device_dir(device) = NULL;
1443 static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
1445 struct acpi_video_bus *video = seq->private;
1451 seq_printf(seq, "Switching heads: %s\n",
1452 video->flags.multihead ? "yes" : "no");
1453 seq_printf(seq, "Video ROM: %s\n",
1454 video->flags.rom ? "yes" : "no");
1455 seq_printf(seq, "Device to be POSTed on boot: %s\n",
1456 video->flags.post ? "yes" : "no");
1462 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
1464 return single_open(file, acpi_video_bus_info_seq_show,
1468 static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
1470 struct acpi_video_bus *video = seq->private;
1476 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
1477 seq_printf(seq, "<TODO>\n");
1483 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
1485 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1488 static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
1490 struct acpi_video_bus *video = seq->private;
1491 unsigned long long options;
1498 status = acpi_video_bus_POST_options(video, &options);
1499 if (ACPI_SUCCESS(status)) {
1500 if (!(options & 1)) {
1501 printk(KERN_WARNING PREFIX
1502 "The motherboard VGA device is not listed as a possible POST device.\n");
1503 printk(KERN_WARNING PREFIX
1504 "This indicates a BIOS bug. Please contact the manufacturer.\n");
1506 printk(KERN_WARNING "%llx\n", options);
1507 seq_printf(seq, "can POST: <integrated video>");
1509 seq_printf(seq, " <PCI video>");
1511 seq_printf(seq, " <AGP video>");
1512 seq_putc(seq, '\n');
1514 seq_printf(seq, "<not supported>\n");
1520 acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
1522 return single_open(file, acpi_video_bus_POST_info_seq_show,
1526 static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
1528 struct acpi_video_bus *video = seq->private;
1530 unsigned long long id;
1536 status = acpi_video_bus_get_POST(video, &id);
1537 if (!ACPI_SUCCESS(status)) {
1538 seq_printf(seq, "<not supported>\n");
1541 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
1547 static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
1549 struct acpi_video_bus *video = seq->private;
1552 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
1557 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
1559 return single_open(file, acpi_video_bus_POST_seq_show,
1563 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
1565 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1569 acpi_video_bus_write_POST(struct file *file,
1570 const char __user * buffer,
1571 size_t count, loff_t * data)
1574 struct seq_file *m = file->private_data;
1575 struct acpi_video_bus *video = m->private;
1576 char str[12] = { 0 };
1577 unsigned long long opt, options;
1580 if (!video || count >= sizeof(str))
1583 status = acpi_video_bus_POST_options(video, &options);
1584 if (!ACPI_SUCCESS(status))
1587 if (copy_from_user(str, buffer, count))
1591 opt = strtoul(str, NULL, 0);
1595 /* just in case an OEM 'forgot' the motherboard... */
1598 if (options & (1ul << opt)) {
1599 status = acpi_video_bus_set_POST(video, opt);
1600 if (!ACPI_SUCCESS(status))
1609 acpi_video_bus_write_DOS(struct file *file,
1610 const char __user * buffer,
1611 size_t count, loff_t * data)
1614 struct seq_file *m = file->private_data;
1615 struct acpi_video_bus *video = m->private;
1616 char str[12] = { 0 };
1620 if (!video || count >= sizeof(str))
1623 if (copy_from_user(str, buffer, count))
1627 opt = strtoul(str, NULL, 0);
1631 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
1633 if (!ACPI_SUCCESS(status))
1639 static int acpi_video_bus_add_fs(struct acpi_device *device)
1641 struct acpi_video_bus *video = acpi_driver_data(device);
1642 struct proc_dir_entry *device_dir;
1643 struct proc_dir_entry *entry;
1645 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1650 entry = proc_create_data("info", S_IRUGO, device_dir,
1651 &acpi_video_bus_info_fops,
1652 acpi_driver_data(device));
1654 goto err_remove_dir;
1657 entry = proc_create_data("ROM", S_IRUGO, device_dir,
1658 &acpi_video_bus_ROM_fops,
1659 acpi_driver_data(device));
1661 goto err_remove_info;
1663 /* 'POST_info' [R] */
1664 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
1665 &acpi_video_bus_POST_info_fops,
1666 acpi_driver_data(device));
1668 goto err_remove_rom;
1671 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
1673 &acpi_video_bus_POST_fops,
1674 acpi_driver_data(device));
1676 goto err_remove_post_info;
1679 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
1681 &acpi_video_bus_DOS_fops,
1682 acpi_driver_data(device));
1684 goto err_remove_post;
1686 video->dir = acpi_device_dir(device) = device_dir;
1690 remove_proc_entry("POST", device_dir);
1691 err_remove_post_info:
1692 remove_proc_entry("POST_info", device_dir);
1694 remove_proc_entry("ROM", device_dir);
1696 remove_proc_entry("info", device_dir);
1698 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1702 static int acpi_video_bus_remove_fs(struct acpi_device *device)
1704 struct proc_dir_entry *device_dir = acpi_device_dir(device);
1707 remove_proc_entry("info", device_dir);
1708 remove_proc_entry("ROM", device_dir);
1709 remove_proc_entry("POST_info", device_dir);
1710 remove_proc_entry("POST", device_dir);
1711 remove_proc_entry("DOS", device_dir);
1712 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1713 acpi_device_dir(device) = NULL;
1719 /* --------------------------------------------------------------------------
1721 -------------------------------------------------------------------------- */
1723 /* device interface */
1724 static struct acpi_video_device_attrib*
1725 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1727 struct acpi_video_enumerated_device *ids;
1730 for (i = 0; i < video->attached_count; i++) {
1731 ids = &video->attached_array[i];
1732 if ((ids->value.int_val & 0xffff) == device_id)
1733 return &ids->value.attrib;
1740 acpi_video_bus_get_one_device(struct acpi_device *device,
1741 struct acpi_video_bus *video)
1743 unsigned long long device_id;
1745 struct acpi_video_device *data;
1746 struct acpi_video_device_attrib* attribute;
1748 if (!device || !video)
1752 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1753 if (ACPI_SUCCESS(status)) {
1755 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1759 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1760 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1761 device->driver_data = data;
1763 data->device_id = device_id;
1764 data->video = video;
1767 attribute = acpi_video_get_device_attr(video, device_id);
1769 if((attribute != NULL) && attribute->device_id_scheme) {
1770 switch (attribute->display_type) {
1771 case ACPI_VIDEO_DISPLAY_CRT:
1772 data->flags.crt = 1;
1774 case ACPI_VIDEO_DISPLAY_TV:
1775 data->flags.tvout = 1;
1777 case ACPI_VIDEO_DISPLAY_DVI:
1778 data->flags.dvi = 1;
1780 case ACPI_VIDEO_DISPLAY_LCD:
1781 data->flags.lcd = 1;
1784 data->flags.unknown = 1;
1787 if(attribute->bios_can_detect)
1788 data->flags.bios = 1;
1790 data->flags.unknown = 1;
1792 acpi_video_device_bind(video, data);
1793 acpi_video_device_find_cap(data);
1795 status = acpi_install_notify_handler(device->handle,
1797 acpi_video_device_notify,
1799 if (ACPI_FAILURE(status)) {
1800 printk(KERN_ERR PREFIX
1801 "Error installing notify handler\n");
1802 if(data->brightness)
1803 kfree(data->brightness->levels);
1804 kfree(data->brightness);
1809 mutex_lock(&video->device_list_lock);
1810 list_add_tail(&data->entry, &video->video_device_list);
1811 mutex_unlock(&video->device_list_lock);
1813 acpi_video_device_add_fs(device);
1823 * video : video bus device
1828 * Enumerate the video device list of the video bus,
1829 * bind the ids with the corresponding video devices
1830 * under the video bus.
1833 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1835 struct acpi_video_device *dev;
1837 mutex_lock(&video->device_list_lock);
1839 list_for_each_entry(dev, &video->video_device_list, entry)
1840 acpi_video_device_bind(video, dev);
1842 mutex_unlock(&video->device_list_lock);
1847 * video : video bus device
1848 * device : video output device under the video
1854 * Bind the ids with the corresponding video devices
1855 * under the video bus.
1859 acpi_video_device_bind(struct acpi_video_bus *video,
1860 struct acpi_video_device *device)
1862 struct acpi_video_enumerated_device *ids;
1865 for (i = 0; i < video->attached_count; i++) {
1866 ids = &video->attached_array[i];
1867 if (device->device_id == (ids->value.int_val & 0xffff)) {
1868 ids->bind_info = device;
1869 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1876 * video : video bus device
1881 * Call _DOD to enumerate all devices attached to display adapter
1885 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1890 struct acpi_video_enumerated_device *active_list;
1891 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1892 union acpi_object *dod = NULL;
1893 union acpi_object *obj;
1895 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1896 if (!ACPI_SUCCESS(status)) {
1897 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1901 dod = buffer.pointer;
1902 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1903 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1908 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1909 dod->package.count));
1911 active_list = kcalloc(1 + dod->package.count,
1912 sizeof(struct acpi_video_enumerated_device),
1920 for (i = 0; i < dod->package.count; i++) {
1921 obj = &dod->package.elements[i];
1923 if (obj->type != ACPI_TYPE_INTEGER) {
1924 printk(KERN_ERR PREFIX
1925 "Invalid _DOD data in element %d\n", i);
1929 active_list[count].value.int_val = obj->integer.value;
1930 active_list[count].bind_info = NULL;
1931 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1932 (int)obj->integer.value));
1936 kfree(video->attached_array);
1938 video->attached_array = active_list;
1939 video->attached_count = count;
1942 kfree(buffer.pointer);
1947 acpi_video_get_next_level(struct acpi_video_device *device,
1948 u32 level_current, u32 event)
1950 int min, max, min_above, max_below, i, l, delta = 255;
1951 max = max_below = 0;
1952 min = min_above = 255;
1953 /* Find closest level to level_current */
1954 for (i = 2; i < device->brightness->count; i++) {
1955 l = device->brightness->levels[i];
1956 if (abs(l - level_current) < abs(delta)) {
1957 delta = l - level_current;
1962 /* Ajust level_current to closest available level */
1963 level_current += delta;
1964 for (i = 2; i < device->brightness->count; i++) {
1965 l = device->brightness->levels[i];
1970 if (l < min_above && l > level_current)
1972 if (l > max_below && l < level_current)
1977 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1978 return (level_current < max) ? min_above : min;
1979 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1980 return (level_current < max) ? min_above : max;
1981 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1982 return (level_current > min) ? max_below : min;
1983 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1984 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1987 return level_current;
1992 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1994 unsigned long long level_current, level_next;
1995 int result = -EINVAL;
1997 /* no warning message if acpi_backlight=vendor is used */
1998 if (!acpi_video_backlight_support())
2001 if (!device->brightness)
2004 result = acpi_video_device_lcd_get_level_current(device,
2009 level_next = acpi_video_get_next_level(device, level_current, event);
2011 result = acpi_video_device_lcd_set_level(device, level_next);
2014 backlight_force_update(device->backlight,
2015 BACKLIGHT_UPDATE_HOTKEY);
2019 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
2025 acpi_video_bus_get_devices(struct acpi_video_bus *video,
2026 struct acpi_device *device)
2029 struct acpi_device *dev;
2031 acpi_video_device_enumerate(video);
2033 list_for_each_entry(dev, &device->children, node) {
2035 status = acpi_video_bus_get_one_device(dev, video);
2036 if (ACPI_FAILURE(status)) {
2037 printk(KERN_WARNING PREFIX
2038 "Cant attach device");
2045 static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
2048 struct acpi_video_bus *video;
2051 if (!device || !device->video)
2054 video = device->video;
2056 acpi_video_device_remove_fs(device->dev);
2058 status = acpi_remove_notify_handler(device->dev->handle,
2060 acpi_video_device_notify);
2061 if (device->backlight) {
2062 sysfs_remove_link(&device->backlight->dev.kobj, "device");
2063 backlight_device_unregister(device->backlight);
2064 device->backlight = NULL;
2066 if (device->cooling_dev) {
2067 sysfs_remove_link(&device->dev->dev.kobj,
2069 sysfs_remove_link(&device->cooling_dev->device.kobj,
2071 thermal_cooling_device_unregister(device->cooling_dev);
2072 device->cooling_dev = NULL;
2074 video_output_unregister(device->output_dev);
2079 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
2082 struct acpi_video_device *dev, *next;
2084 mutex_lock(&video->device_list_lock);
2086 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
2088 status = acpi_video_bus_put_one_device(dev);
2089 if (ACPI_FAILURE(status))
2090 printk(KERN_WARNING PREFIX
2091 "hhuuhhuu bug in acpi video driver.\n");
2093 if (dev->brightness) {
2094 kfree(dev->brightness->levels);
2095 kfree(dev->brightness);
2097 list_del(&dev->entry);
2101 mutex_unlock(&video->device_list_lock);
2106 /* acpi_video interface */
2108 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
2110 return acpi_video_bus_DOS(video, 0, 0);
2113 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
2115 return acpi_video_bus_DOS(video, 0, 1);
2118 static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
2120 struct acpi_video_bus *video = acpi_driver_data(device);
2121 struct input_dev *input;
2127 input = video->input;
2130 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
2131 * most likely via hotkey. */
2132 acpi_bus_generate_proc_event(device, event, 0);
2133 keycode = KEY_SWITCHVIDEOMODE;
2136 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
2138 acpi_video_device_enumerate(video);
2139 acpi_video_device_rebind(video);
2140 acpi_bus_generate_proc_event(device, event, 0);
2141 keycode = KEY_SWITCHVIDEOMODE;
2144 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
2145 acpi_bus_generate_proc_event(device, event, 0);
2146 keycode = KEY_SWITCHVIDEOMODE;
2148 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
2149 acpi_bus_generate_proc_event(device, event, 0);
2150 keycode = KEY_VIDEO_NEXT;
2152 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
2153 acpi_bus_generate_proc_event(device, event, 0);
2154 keycode = KEY_VIDEO_PREV;
2158 keycode = KEY_UNKNOWN;
2159 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2160 "Unsupported event [0x%x]\n", event));
2164 acpi_notifier_call_chain(device, event, 0);
2165 input_report_key(input, keycode, 1);
2167 input_report_key(input, keycode, 0);
2173 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
2175 struct acpi_video_device *video_device = data;
2176 struct acpi_device *device = NULL;
2177 struct acpi_video_bus *bus;
2178 struct input_dev *input;
2184 device = video_device->dev;
2185 bus = video_device->video;
2189 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
2190 if (brightness_switch_enabled)
2191 acpi_video_switch_brightness(video_device, event);
2192 acpi_bus_generate_proc_event(device, event, 0);
2193 keycode = KEY_BRIGHTNESS_CYCLE;
2195 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
2196 if (brightness_switch_enabled)
2197 acpi_video_switch_brightness(video_device, event);
2198 acpi_bus_generate_proc_event(device, event, 0);
2199 keycode = KEY_BRIGHTNESSUP;
2201 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
2202 if (brightness_switch_enabled)
2203 acpi_video_switch_brightness(video_device, event);
2204 acpi_bus_generate_proc_event(device, event, 0);
2205 keycode = KEY_BRIGHTNESSDOWN;
2207 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
2208 if (brightness_switch_enabled)
2209 acpi_video_switch_brightness(video_device, event);
2210 acpi_bus_generate_proc_event(device, event, 0);
2211 keycode = KEY_BRIGHTNESS_ZERO;
2213 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
2214 if (brightness_switch_enabled)
2215 acpi_video_switch_brightness(video_device, event);
2216 acpi_bus_generate_proc_event(device, event, 0);
2217 keycode = KEY_DISPLAY_OFF;
2220 keycode = KEY_UNKNOWN;
2221 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2222 "Unsupported event [0x%x]\n", event));
2226 acpi_notifier_call_chain(device, event, 0);
2227 input_report_key(input, keycode, 1);
2229 input_report_key(input, keycode, 0);
2235 static int instance;
2236 static int acpi_video_resume(struct acpi_device *device)
2238 struct acpi_video_bus *video;
2239 struct acpi_video_device *video_device;
2242 if (!device || !acpi_driver_data(device))
2245 video = acpi_driver_data(device);
2247 for (i = 0; i < video->attached_count; i++) {
2248 video_device = video->attached_array[i].bind_info;
2249 if (video_device && video_device->backlight)
2250 acpi_video_set_brightness(video_device->backlight);
2256 acpi_video_bus_match(acpi_handle handle, u32 level, void *context,
2257 void **return_value)
2259 struct acpi_device *device = context;
2260 struct acpi_device *sibling;
2263 if (handle == device->handle)
2264 return AE_CTRL_TERMINATE;
2266 result = acpi_bus_get_device(handle, &sibling);
2270 if (!strcmp(acpi_device_name(sibling), ACPI_VIDEO_BUS_NAME))
2271 return AE_ALREADY_EXISTS;
2276 static int acpi_video_bus_add(struct acpi_device *device)
2278 struct acpi_video_bus *video;
2279 struct input_dev *input;
2283 status = acpi_walk_namespace(ACPI_TYPE_DEVICE,
2284 device->parent->handle, 1,
2285 acpi_video_bus_match, NULL,
2287 if (status == AE_ALREADY_EXISTS) {
2288 printk(KERN_WARNING FW_BUG
2289 "Duplicate ACPI video bus devices for the"
2290 " same VGA controller, please try module "
2291 "parameter \"video.allow_duplicates=1\""
2292 "if the current driver doesn't work.\n");
2293 if (!allow_duplicates)
2297 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
2301 /* a hack to fix the duplicate name "VID" problem on T61 */
2302 if (!strcmp(device->pnp.bus_id, "VID")) {
2304 device->pnp.bus_id[3] = '0' + instance;
2307 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2308 if (!strcmp(device->pnp.bus_id, "VGA")) {
2310 device->pnp.bus_id[3] = '0' + instance;
2314 video->device = device;
2315 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2316 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
2317 device->driver_data = video;
2319 acpi_video_bus_find_cap(video);
2320 error = acpi_video_bus_check(video);
2322 goto err_free_video;
2324 error = acpi_video_bus_add_fs(device);
2326 goto err_free_video;
2328 mutex_init(&video->device_list_lock);
2329 INIT_LIST_HEAD(&video->video_device_list);
2331 acpi_video_bus_get_devices(video, device);
2332 acpi_video_bus_start_devices(video);
2334 video->input = input = input_allocate_device();
2337 goto err_stop_video;
2340 snprintf(video->phys, sizeof(video->phys),
2341 "%s/video/input0", acpi_device_hid(video->device));
2343 input->name = acpi_device_name(video->device);
2344 input->phys = video->phys;
2345 input->id.bustype = BUS_HOST;
2346 input->id.product = 0x06;
2347 input->dev.parent = &device->dev;
2348 input->evbit[0] = BIT(EV_KEY);
2349 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2350 set_bit(KEY_VIDEO_NEXT, input->keybit);
2351 set_bit(KEY_VIDEO_PREV, input->keybit);
2352 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2353 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2354 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2355 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2356 set_bit(KEY_DISPLAY_OFF, input->keybit);
2357 set_bit(KEY_UNKNOWN, input->keybit);
2359 error = input_register_device(input);
2361 goto err_free_input_dev;
2363 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
2364 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2365 video->flags.multihead ? "yes" : "no",
2366 video->flags.rom ? "yes" : "no",
2367 video->flags.post ? "yes" : "no");
2372 input_free_device(input);
2374 acpi_video_bus_stop_devices(video);
2375 acpi_video_bus_put_devices(video);
2376 kfree(video->attached_array);
2377 acpi_video_bus_remove_fs(device);
2380 device->driver_data = NULL;
2385 static int acpi_video_bus_remove(struct acpi_device *device, int type)
2387 struct acpi_video_bus *video = NULL;
2390 if (!device || !acpi_driver_data(device))
2393 video = acpi_driver_data(device);
2395 acpi_video_bus_stop_devices(video);
2396 acpi_video_bus_put_devices(video);
2397 acpi_video_bus_remove_fs(device);
2399 input_unregister_device(video->input);
2400 kfree(video->attached_array);
2406 static int __init intel_opregion_present(void)
2408 #if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2409 struct pci_dev *dev = NULL;
2412 for_each_pci_dev(dev) {
2413 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2415 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2417 pci_read_config_dword(dev, 0xfc, &address);
2426 int acpi_video_register(void)
2429 if (register_count) {
2431 * if the function of acpi_video_register is already called,
2432 * don't register the acpi_vide_bus again and return no error.
2437 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2438 if (!acpi_video_dir)
2441 result = acpi_bus_register_driver(&acpi_video_bus);
2443 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2448 * When the acpi_video_bus is loaded successfully, increase
2449 * the counter reference.
2455 EXPORT_SYMBOL(acpi_video_register);
2457 void acpi_video_unregister(void)
2459 if (!register_count) {
2461 * If the acpi video bus is already unloaded, don't
2462 * unload it again and return directly.
2466 acpi_bus_unregister_driver(&acpi_video_bus);
2468 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2474 EXPORT_SYMBOL(acpi_video_unregister);
2477 * This is kind of nasty. Hardware using Intel chipsets may require
2478 * the video opregion code to be run first in order to initialise
2479 * state before any ACPI video calls are made. To handle this we defer
2480 * registration of the video class until the opregion code has run.
2483 static int __init acpi_video_init(void)
2485 dmi_check_system(video_dmi_table);
2487 if (intel_opregion_present())
2490 return acpi_video_register();
2493 static void __exit acpi_video_exit(void)
2495 acpi_video_unregister();
2500 module_init(acpi_video_init);
2501 module_exit(acpi_video_exit);