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>
44 #include <acpi/acpi_bus.h>
45 #include <acpi/acpi_drivers.h>
47 #define ACPI_VIDEO_CLASS "video"
48 #define ACPI_VIDEO_BUS_NAME "Video Bus"
49 #define ACPI_VIDEO_DEVICE_NAME "Video Device"
50 #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
51 #define ACPI_VIDEO_NOTIFY_PROBE 0x81
52 #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
53 #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
54 #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
56 #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
57 #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
58 #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
59 #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
60 #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
62 #define MAX_NAME_LEN 20
64 #define ACPI_VIDEO_DISPLAY_CRT 1
65 #define ACPI_VIDEO_DISPLAY_TV 2
66 #define ACPI_VIDEO_DISPLAY_DVI 3
67 #define ACPI_VIDEO_DISPLAY_LCD 4
69 #define _COMPONENT ACPI_VIDEO_COMPONENT
70 ACPI_MODULE_NAME("video");
72 MODULE_AUTHOR("Bruno Ducrot");
73 MODULE_DESCRIPTION("ACPI Video Driver");
74 MODULE_LICENSE("GPL");
76 static int brightness_switch_enabled = 1;
77 module_param(brightness_switch_enabled, bool, 0644);
79 static int acpi_video_bus_add(struct acpi_device *device);
80 static int acpi_video_bus_remove(struct acpi_device *device, int type);
81 static int acpi_video_resume(struct acpi_device *device);
82 static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
84 static const struct acpi_device_id video_device_ids[] = {
88 MODULE_DEVICE_TABLE(acpi, video_device_ids);
90 static struct acpi_driver acpi_video_bus = {
92 .class = ACPI_VIDEO_CLASS,
93 .ids = video_device_ids,
95 .add = acpi_video_bus_add,
96 .remove = acpi_video_bus_remove,
97 .resume = acpi_video_resume,
98 .notify = acpi_video_bus_notify,
102 struct acpi_video_bus_flags {
103 u8 multihead:1; /* can switch video heads */
104 u8 rom:1; /* can retrieve a video rom */
105 u8 post:1; /* can configure the head to */
109 struct acpi_video_bus_cap {
110 u8 _DOS:1; /*Enable/Disable output switching */
111 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
112 u8 _ROM:1; /*Get ROM Data */
113 u8 _GPD:1; /*Get POST Device */
114 u8 _SPD:1; /*Set POST Device */
115 u8 _VPO:1; /*Video POST Options */
119 struct acpi_video_device_attrib {
120 u32 display_index:4; /* A zero-based instance of the Display */
121 u32 display_port_attachment:4; /*This field differentiates the display type */
122 u32 display_type:4; /*Describe the specific type in use */
123 u32 vendor_specific:4; /*Chipset Vendor Specific */
124 u32 bios_can_detect:1; /*BIOS can detect the device */
125 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
127 u32 pipe_id:3; /*For VGA multiple-head devices. */
128 u32 reserved:10; /*Must be 0 */
129 u32 device_id_scheme:1; /*Device ID Scheme */
132 struct acpi_video_enumerated_device {
135 struct acpi_video_device_attrib attrib;
137 struct acpi_video_device *bind_info;
140 struct acpi_video_bus {
141 struct acpi_device *device;
143 struct acpi_video_enumerated_device *attached_array;
145 struct acpi_video_bus_cap cap;
146 struct acpi_video_bus_flags flags;
147 struct list_head video_device_list;
148 struct mutex device_list_lock; /* protects video_device_list */
149 struct proc_dir_entry *dir;
150 struct input_dev *input;
151 char phys[32]; /* for input device */
154 struct acpi_video_device_flags {
164 struct acpi_video_device_cap {
165 u8 _ADR:1; /*Return the unique ID */
166 u8 _BCL:1; /*Query list of brightness control levels supported */
167 u8 _BCM:1; /*Set the brightness level */
168 u8 _BQC:1; /* Get current brightness level */
169 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
170 u8 _DDC:1; /*Return the EDID for this device */
171 u8 _DCS:1; /*Return status of output device */
172 u8 _DGS:1; /*Query graphics state */
173 u8 _DSS:1; /*Device state set */
176 struct acpi_video_brightness_flags {
177 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
178 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
179 u8 _BCL_use_index:1; /* levels in _BCL are index values */
180 u8 _BCM_use_index:1; /* input of _BCM is an index value */
181 u8 _BQC_use_index:1; /* _BQC returns an index value */
184 struct acpi_video_device_brightness {
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 *cdev;
201 struct output_device *output_dev;
205 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
206 static const struct file_operations acpi_video_bus_info_fops = {
207 .owner = THIS_MODULE,
208 .open = acpi_video_bus_info_open_fs,
211 .release = single_release,
214 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
215 static const struct file_operations acpi_video_bus_ROM_fops = {
216 .owner = THIS_MODULE,
217 .open = acpi_video_bus_ROM_open_fs,
220 .release = single_release,
223 static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
225 static const struct file_operations acpi_video_bus_POST_info_fops = {
226 .owner = THIS_MODULE,
227 .open = acpi_video_bus_POST_info_open_fs,
230 .release = single_release,
233 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
234 static ssize_t acpi_video_bus_write_POST(struct file *file,
235 const char __user *buffer, size_t count, loff_t *data);
236 static const struct file_operations acpi_video_bus_POST_fops = {
237 .owner = THIS_MODULE,
238 .open = acpi_video_bus_POST_open_fs,
240 .write = acpi_video_bus_write_POST,
242 .release = single_release,
245 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
246 static ssize_t acpi_video_bus_write_DOS(struct file *file,
247 const char __user *buffer, size_t count, loff_t *data);
248 static const struct file_operations acpi_video_bus_DOS_fops = {
249 .owner = THIS_MODULE,
250 .open = acpi_video_bus_DOS_open_fs,
252 .write = acpi_video_bus_write_DOS,
254 .release = single_release,
258 static int acpi_video_device_info_open_fs(struct inode *inode,
260 static const struct file_operations acpi_video_device_info_fops = {
261 .owner = THIS_MODULE,
262 .open = acpi_video_device_info_open_fs,
265 .release = single_release,
268 static int acpi_video_device_state_open_fs(struct inode *inode,
270 static ssize_t acpi_video_device_write_state(struct file *file,
271 const char __user *buffer, size_t count, loff_t *data);
272 static const struct file_operations acpi_video_device_state_fops = {
273 .owner = THIS_MODULE,
274 .open = acpi_video_device_state_open_fs,
276 .write = acpi_video_device_write_state,
278 .release = single_release,
281 static int acpi_video_device_brightness_open_fs(struct inode *inode,
283 static ssize_t acpi_video_device_write_brightness(struct file *file,
284 const char __user *buffer, size_t count, loff_t *data);
285 static struct file_operations acpi_video_device_brightness_fops = {
286 .owner = THIS_MODULE,
287 .open = acpi_video_device_brightness_open_fs,
289 .write = acpi_video_device_write_brightness,
291 .release = single_release,
294 static int acpi_video_device_EDID_open_fs(struct inode *inode,
296 static const struct file_operations acpi_video_device_EDID_fops = {
297 .owner = THIS_MODULE,
298 .open = acpi_video_device_EDID_open_fs,
301 .release = single_release,
304 static const char device_decode[][30] = {
305 "motherboard VGA device",
311 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
312 static void acpi_video_device_rebind(struct acpi_video_bus *video);
313 static void acpi_video_device_bind(struct acpi_video_bus *video,
314 struct acpi_video_device *device);
315 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
316 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
318 static int acpi_video_device_lcd_get_level_current(
319 struct acpi_video_device *device,
320 unsigned long long *level);
321 static int acpi_video_get_next_level(struct acpi_video_device *device,
322 u32 level_current, u32 event);
323 static int acpi_video_switch_brightness(struct acpi_video_device *device,
325 static int acpi_video_device_get_state(struct acpi_video_device *device,
326 unsigned long long *state);
327 static int acpi_video_output_get(struct output_device *od);
328 static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
330 /*backlight device sysfs support*/
331 static int acpi_video_get_brightness(struct backlight_device *bd)
333 unsigned long long cur_level;
335 struct acpi_video_device *vd =
336 (struct acpi_video_device *)bl_get_data(bd);
338 if (acpi_video_device_lcd_get_level_current(vd, &cur_level))
340 for (i = 2; i < vd->brightness->count; i++) {
341 if (vd->brightness->levels[i] == cur_level)
342 /* The first two entries are special - see page 575
343 of the ACPI spec 3.0 */
349 static int acpi_video_set_brightness(struct backlight_device *bd)
351 int request_level = bd->props.brightness + 2;
352 struct acpi_video_device *vd =
353 (struct acpi_video_device *)bl_get_data(bd);
355 return acpi_video_device_lcd_set_level(vd,
356 vd->brightness->levels[request_level]);
359 static struct backlight_ops acpi_backlight_ops = {
360 .get_brightness = acpi_video_get_brightness,
361 .update_status = acpi_video_set_brightness,
364 /*video output device sysfs support*/
365 static int acpi_video_output_get(struct output_device *od)
367 unsigned long long state;
368 struct acpi_video_device *vd =
369 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
370 acpi_video_device_get_state(vd, &state);
374 static int acpi_video_output_set(struct output_device *od)
376 unsigned long state = od->request_state;
377 struct acpi_video_device *vd=
378 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
379 return acpi_video_device_set_state(vd, state);
382 static struct output_properties acpi_output_properties = {
383 .set_state = acpi_video_output_set,
384 .get_status = acpi_video_output_get,
388 /* thermal cooling device callbacks */
389 static int video_get_max_state(struct thermal_cooling_device *cdev, unsigned
392 struct acpi_device *device = cdev->devdata;
393 struct acpi_video_device *video = acpi_driver_data(device);
395 *state = video->brightness->count - 3;
399 static int video_get_cur_state(struct thermal_cooling_device *cdev, unsigned
402 struct acpi_device *device = cdev->devdata;
403 struct acpi_video_device *video = acpi_driver_data(device);
404 unsigned long long level;
407 if (acpi_video_device_lcd_get_level_current(video, &level))
409 for (offset = 2; offset < video->brightness->count; offset++)
410 if (level == video->brightness->levels[offset]) {
411 *state = video->brightness->count - offset - 1;
419 video_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
421 struct acpi_device *device = cdev->devdata;
422 struct acpi_video_device *video = acpi_driver_data(device);
425 if ( state >= video->brightness->count - 2)
428 state = video->brightness->count - state;
429 level = video->brightness->levels[state -1];
430 return acpi_video_device_lcd_set_level(video, level);
433 static struct thermal_cooling_device_ops video_cooling_ops = {
434 .get_max_state = video_get_max_state,
435 .get_cur_state = video_get_cur_state,
436 .set_cur_state = video_set_cur_state,
439 /* --------------------------------------------------------------------------
441 -------------------------------------------------------------------------- */
446 acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
450 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
456 acpi_video_device_get_state(struct acpi_video_device *device,
457 unsigned long long *state)
461 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
467 acpi_video_device_set_state(struct acpi_video_device *device, int state)
470 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
471 struct acpi_object_list args = { 1, &arg0 };
472 unsigned long long ret;
475 arg0.integer.value = state;
476 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
482 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
483 union acpi_object **levels)
486 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
487 union acpi_object *obj;
492 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
493 if (!ACPI_SUCCESS(status))
495 obj = (union acpi_object *)buffer.pointer;
496 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
497 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
507 kfree(buffer.pointer);
513 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
516 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
517 struct acpi_object_list args = { 1, &arg0 };
520 arg0.integer.value = level;
522 status = acpi_evaluate_object(device->dev->handle, "_BCM",
524 if (ACPI_FAILURE(status)) {
525 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
529 device->brightness->curr = level;
530 for (state = 2; state < device->brightness->count; state++)
531 if (level == device->brightness->levels[state]) {
532 if (device->backlight)
533 device->backlight->props.brightness = state - 2;
537 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
542 * For some buggy _BQC methods, we need to add a constant value to
543 * the _BQC return value to get the actual current brightness level
546 static int bqc_offset_aml_bug_workaround;
547 static int __init video_set_bqc_offset(const struct dmi_system_id *d)
549 bqc_offset_aml_bug_workaround = 9;
553 static struct dmi_system_id video_dmi_table[] __initdata = {
555 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
558 .callback = video_set_bqc_offset,
559 .ident = "Acer Aspire 5720",
561 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
562 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
569 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
570 unsigned long long *level)
572 acpi_status status = AE_OK;
574 if (device->cap._BQC || device->cap._BCQ) {
575 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
577 status = acpi_evaluate_integer(device->dev->handle, buf,
579 if (ACPI_SUCCESS(status)) {
580 if (device->brightness->flags._BQC_use_index) {
581 if (device->brightness->flags._BCL_reversed)
582 *level = device->brightness->count
584 *level = device->brightness->levels[*level + 2];
587 *level += bqc_offset_aml_bug_workaround;
588 device->brightness->curr = *level;
592 * should we return an error or ignore this failure?
593 * dev->brightness->curr is a cached value which stores
594 * the correct current backlight level in most cases.
595 * ACPI video backlight still works w/ buggy _BQC.
596 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
598 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
599 device->cap._BQC = device->cap._BCQ = 0;
603 *level = device->brightness->curr;
608 acpi_video_device_EDID(struct acpi_video_device *device,
609 union acpi_object **edid, ssize_t length)
612 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
613 union acpi_object *obj;
614 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
615 struct acpi_object_list args = { 1, &arg0 };
623 arg0.integer.value = 1;
624 else if (length == 256)
625 arg0.integer.value = 2;
629 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
630 if (ACPI_FAILURE(status))
633 obj = buffer.pointer;
635 if (obj && obj->type == ACPI_TYPE_BUFFER)
638 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
649 acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
652 unsigned long long tmp;
653 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
654 struct acpi_object_list args = { 1, &arg0 };
657 arg0.integer.value = option;
659 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
660 if (ACPI_SUCCESS(status))
661 status = tmp ? (-EINVAL) : (AE_OK);
667 acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
671 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
677 acpi_video_bus_POST_options(struct acpi_video_bus *video,
678 unsigned long long *options)
682 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
690 * video : video bus device pointer
692 * 0. The system BIOS should NOT automatically switch(toggle)
693 * the active display output.
694 * 1. The system BIOS should automatically switch (toggle) the
695 * active display output. No switch event.
696 * 2. The _DGS value should be locked.
697 * 3. The system BIOS should not automatically switch (toggle) the
698 * active display output, but instead generate the display switch
701 * 0. The system BIOS should automatically control the brightness level
702 * of the LCD when the power changes from AC to DC
703 * 1. The system BIOS should NOT automatically control the brightness
704 * level of the LCD when the power changes from AC to DC.
710 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
712 acpi_integer status = 0;
713 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
714 struct acpi_object_list args = { 1, &arg0 };
717 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
721 arg0.integer.value = (lcd_flag << 2) | bios_flag;
722 video->dos_setting = arg0.integer.value;
723 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
730 * Simple comparison function used to sort backlight levels.
734 acpi_video_cmp_level(const void *a, const void *b)
736 return *(int *)a - *(int *)b;
741 * device : video output device (LCD, CRT, ..)
744 * Maximum brightness level
746 * Allocate and initialize device->brightness.
750 acpi_video_init_brightness(struct acpi_video_device *device)
752 union acpi_object *obj = NULL;
753 int i, max_level = 0, count = 0, level_ac_battery = 0;
754 unsigned long long level, level_old;
755 union acpi_object *o;
756 struct acpi_video_device_brightness *br = NULL;
757 int result = -EINVAL;
759 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
760 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
761 "LCD brightness level\n"));
765 if (obj->package.count < 2)
768 br = kzalloc(sizeof(*br), GFP_KERNEL);
770 printk(KERN_ERR "can't allocate memory\n");
775 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
782 for (i = 0; i < obj->package.count; i++) {
783 o = (union acpi_object *)&obj->package.elements[i];
784 if (o->type != ACPI_TYPE_INTEGER) {
785 printk(KERN_ERR PREFIX "Invalid data\n");
788 br->levels[count] = (u32) o->integer.value;
790 if (br->levels[count] > max_level)
791 max_level = br->levels[count];
796 * some buggy BIOS don't export the levels
797 * when machine is on AC/Battery in _BCL package.
798 * In this case, the first two elements in _BCL packages
799 * are also supported brightness levels that OS should take care of.
801 for (i = 2; i < count; i++)
802 if (br->levels[i] == br->levels[0] ||
803 br->levels[i] == br->levels[1])
806 if (level_ac_battery < 2) {
807 level_ac_battery = 2 - level_ac_battery;
808 br->flags._BCL_no_ac_battery_levels = 1;
809 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
810 br->levels[i] = br->levels[i - level_ac_battery];
811 count += level_ac_battery;
812 } else if (level_ac_battery > 2)
813 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
815 /* Check if the _BCL package is in a reversed order */
816 if (max_level == br->levels[2]) {
817 br->flags._BCL_reversed = 1;
818 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
819 acpi_video_cmp_level, NULL);
820 } else if (max_level != br->levels[count - 1])
822 "Found unordered _BCL package\n"));
825 device->brightness = br;
827 /* Check the input/output of _BQC/_BCL/_BCM */
828 if ((max_level < 100) && (max_level <= (count - 2)))
829 br->flags._BCL_use_index = 1;
832 * _BCM is always consistent with _BCL,
833 * at least for all the laptops we have ever seen.
835 br->flags._BCM_use_index = br->flags._BCL_use_index;
837 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
838 br->curr = max_level;
839 result = acpi_video_device_lcd_get_level_current(device, &level_old);
841 goto out_free_levels;
843 result = acpi_video_device_lcd_set_level(device, br->curr);
845 goto out_free_levels;
847 result = acpi_video_device_lcd_get_level_current(device, &level);
849 goto out_free_levels;
851 if ((level != level_old) && !br->flags._BCM_use_index) {
853 * This piece of code does not work correctly if the current
854 * brightness levels is 0.
855 * But I guess boxes that boot with such a dark screen are rare
856 * and no more code is needed to cover this specifial case.
859 if (level_ac_battery != 2) {
861 * For now, we don't support the _BCL like this:
862 * 16, 15, 0, 1, 2, 3, ..., 14, 15, 16
863 * because we may mess up the index returned by _BQC.
864 * Plus: we have not got a box like this.
866 ACPI_ERROR((AE_INFO, "_BCL not supported\n"));
868 br->flags._BQC_use_index = 1;
871 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
872 "found %d brightness levels\n", count - 2));
881 device->brightness = NULL;
888 * device : video output device (LCD, CRT, ..)
893 * Find out all required AML methods defined under the output
897 static void acpi_video_device_find_cap(struct acpi_video_device *device)
899 acpi_handle h_dummy1;
902 memset(&device->cap, 0, sizeof(device->cap));
904 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
905 device->cap._ADR = 1;
907 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
908 device->cap._BCL = 1;
910 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
911 device->cap._BCM = 1;
913 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
914 device->cap._BQC = 1;
915 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
917 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
918 device->cap._BCQ = 1;
921 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
922 device->cap._DDC = 1;
924 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
925 device->cap._DCS = 1;
927 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
928 device->cap._DGS = 1;
930 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
931 device->cap._DSS = 1;
934 if (acpi_video_backlight_support()) {
936 static int count = 0;
939 result = acpi_video_init_brightness(device);
942 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
946 sprintf(name, "acpi_video%d", count++);
947 device->backlight = backlight_device_register(name,
948 NULL, device, &acpi_backlight_ops);
949 device->backlight->props.max_brightness = device->brightness->count-3;
952 device->cdev = thermal_cooling_device_register("LCD",
953 device->dev, &video_cooling_ops);
954 if (IS_ERR(device->cdev))
957 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
959 result = sysfs_create_link(&device->dev->dev.kobj,
960 &device->cdev->device.kobj,
963 printk(KERN_ERR PREFIX "Create sysfs link\n");
964 result = sysfs_create_link(&device->cdev->device.kobj,
965 &device->dev->dev.kobj, "device");
967 printk(KERN_ERR PREFIX "Create sysfs link\n");
971 if (acpi_video_display_switch_support()) {
973 if (device->cap._DCS && device->cap._DSS) {
976 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
979 sprintf(name, "acpi_video%d", count++);
980 device->output_dev = video_output_register(name,
981 NULL, device, &acpi_output_properties);
989 * device : video output device (VGA)
994 * Find out all required AML methods defined under the video bus device.
997 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
999 acpi_handle h_dummy1;
1001 memset(&video->cap, 0, sizeof(video->cap));
1002 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
1003 video->cap._DOS = 1;
1005 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
1006 video->cap._DOD = 1;
1008 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
1009 video->cap._ROM = 1;
1011 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
1012 video->cap._GPD = 1;
1014 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
1015 video->cap._SPD = 1;
1017 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
1018 video->cap._VPO = 1;
1023 * Check whether the video bus device has required AML method to
1024 * support the desired features
1027 static int acpi_video_bus_check(struct acpi_video_bus *video)
1029 acpi_status status = -ENOENT;
1035 dev = acpi_get_physical_pci_device(video->device->handle);
1040 /* Since there is no HID, CID and so on for VGA driver, we have
1041 * to check well known required nodes.
1044 /* Does this device support video switching? */
1045 if (video->cap._DOS) {
1046 video->flags.multihead = 1;
1050 /* Does this device support retrieving a video ROM? */
1051 if (video->cap._ROM) {
1052 video->flags.rom = 1;
1056 /* Does this device support configuring which video device to POST? */
1057 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
1058 video->flags.post = 1;
1065 /* --------------------------------------------------------------------------
1066 FS Interface (/proc)
1067 -------------------------------------------------------------------------- */
1069 static struct proc_dir_entry *acpi_video_dir;
1073 static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
1075 struct acpi_video_device *dev = seq->private;
1081 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1082 seq_printf(seq, "type: ");
1084 seq_printf(seq, "CRT\n");
1085 else if (dev->flags.lcd)
1086 seq_printf(seq, "LCD\n");
1087 else if (dev->flags.tvout)
1088 seq_printf(seq, "TVOUT\n");
1089 else if (dev->flags.dvi)
1090 seq_printf(seq, "DVI\n");
1092 seq_printf(seq, "UNKNOWN\n");
1094 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
1101 acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
1103 return single_open(file, acpi_video_device_info_seq_show,
1107 static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
1110 struct acpi_video_device *dev = seq->private;
1111 unsigned long long state;
1117 status = acpi_video_device_get_state(dev, &state);
1118 seq_printf(seq, "state: ");
1119 if (ACPI_SUCCESS(status))
1120 seq_printf(seq, "0x%02llx\n", state);
1122 seq_printf(seq, "<not supported>\n");
1124 status = acpi_video_device_query(dev, &state);
1125 seq_printf(seq, "query: ");
1126 if (ACPI_SUCCESS(status))
1127 seq_printf(seq, "0x%02llx\n", state);
1129 seq_printf(seq, "<not supported>\n");
1136 acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
1138 return single_open(file, acpi_video_device_state_seq_show,
1143 acpi_video_device_write_state(struct file *file,
1144 const char __user * buffer,
1145 size_t count, loff_t * data)
1148 struct seq_file *m = file->private_data;
1149 struct acpi_video_device *dev = m->private;
1150 char str[12] = { 0 };
1154 if (!dev || count + 1 > sizeof str)
1157 if (copy_from_user(str, buffer, count))
1161 state = simple_strtoul(str, NULL, 0);
1162 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
1164 status = acpi_video_device_set_state(dev, state);
1173 acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
1175 struct acpi_video_device *dev = seq->private;
1179 if (!dev || !dev->brightness) {
1180 seq_printf(seq, "<not supported>\n");
1184 seq_printf(seq, "levels: ");
1185 for (i = 2; i < dev->brightness->count; i++)
1186 seq_printf(seq, " %d", dev->brightness->levels[i]);
1187 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1193 acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
1195 return single_open(file, acpi_video_device_brightness_seq_show,
1200 acpi_video_device_write_brightness(struct file *file,
1201 const char __user * buffer,
1202 size_t count, loff_t * data)
1204 struct seq_file *m = file->private_data;
1205 struct acpi_video_device *dev = m->private;
1206 char str[5] = { 0 };
1207 unsigned int level = 0;
1211 if (!dev || !dev->brightness || count + 1 > sizeof str)
1214 if (copy_from_user(str, buffer, count))
1218 level = simple_strtoul(str, NULL, 0);
1223 /* validate through the list of available levels */
1224 for (i = 2; i < dev->brightness->count; i++)
1225 if (level == dev->brightness->levels[i]) {
1226 if (!acpi_video_device_lcd_set_level(dev, level))
1234 static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
1236 struct acpi_video_device *dev = seq->private;
1239 union acpi_object *edid = NULL;
1245 status = acpi_video_device_EDID(dev, &edid, 128);
1246 if (ACPI_FAILURE(status)) {
1247 status = acpi_video_device_EDID(dev, &edid, 256);
1250 if (ACPI_FAILURE(status)) {
1254 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1255 for (i = 0; i < edid->buffer.length; i++)
1256 seq_putc(seq, edid->buffer.pointer[i]);
1261 seq_printf(seq, "<not supported>\n");
1269 acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
1271 return single_open(file, acpi_video_device_EDID_seq_show,
1275 static int acpi_video_device_add_fs(struct acpi_device *device)
1277 struct proc_dir_entry *entry, *device_dir;
1278 struct acpi_video_device *vid_dev;
1280 vid_dev = acpi_driver_data(device);
1284 device_dir = proc_mkdir(acpi_device_bid(device),
1285 vid_dev->video->dir);
1290 entry = proc_create_data("info", S_IRUGO, device_dir,
1291 &acpi_video_device_info_fops, acpi_driver_data(device));
1293 goto err_remove_dir;
1296 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
1298 &acpi_video_device_state_fops,
1299 acpi_driver_data(device));
1301 goto err_remove_info;
1303 /* 'brightness' [R/W] */
1304 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
1306 &acpi_video_device_brightness_fops,
1307 acpi_driver_data(device));
1309 goto err_remove_state;
1312 entry = proc_create_data("EDID", S_IRUGO, device_dir,
1313 &acpi_video_device_EDID_fops,
1314 acpi_driver_data(device));
1316 goto err_remove_brightness;
1318 acpi_device_dir(device) = device_dir;
1322 err_remove_brightness:
1323 remove_proc_entry("brightness", device_dir);
1325 remove_proc_entry("state", device_dir);
1327 remove_proc_entry("info", device_dir);
1329 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1333 static int acpi_video_device_remove_fs(struct acpi_device *device)
1335 struct acpi_video_device *vid_dev;
1336 struct proc_dir_entry *device_dir;
1338 vid_dev = acpi_driver_data(device);
1339 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
1342 device_dir = acpi_device_dir(device);
1344 remove_proc_entry("info", device_dir);
1345 remove_proc_entry("state", device_dir);
1346 remove_proc_entry("brightness", device_dir);
1347 remove_proc_entry("EDID", device_dir);
1348 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1349 acpi_device_dir(device) = NULL;
1356 static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
1358 struct acpi_video_bus *video = seq->private;
1364 seq_printf(seq, "Switching heads: %s\n",
1365 video->flags.multihead ? "yes" : "no");
1366 seq_printf(seq, "Video ROM: %s\n",
1367 video->flags.rom ? "yes" : "no");
1368 seq_printf(seq, "Device to be POSTed on boot: %s\n",
1369 video->flags.post ? "yes" : "no");
1375 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
1377 return single_open(file, acpi_video_bus_info_seq_show,
1381 static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
1383 struct acpi_video_bus *video = seq->private;
1389 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
1390 seq_printf(seq, "<TODO>\n");
1396 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
1398 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1401 static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
1403 struct acpi_video_bus *video = seq->private;
1404 unsigned long long options;
1411 status = acpi_video_bus_POST_options(video, &options);
1412 if (ACPI_SUCCESS(status)) {
1413 if (!(options & 1)) {
1414 printk(KERN_WARNING PREFIX
1415 "The motherboard VGA device is not listed as a possible POST device.\n");
1416 printk(KERN_WARNING PREFIX
1417 "This indicates a BIOS bug. Please contact the manufacturer.\n");
1419 printk(KERN_WARNING "%llx\n", options);
1420 seq_printf(seq, "can POST: <integrated video>");
1422 seq_printf(seq, " <PCI video>");
1424 seq_printf(seq, " <AGP video>");
1425 seq_putc(seq, '\n');
1427 seq_printf(seq, "<not supported>\n");
1433 acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
1435 return single_open(file, acpi_video_bus_POST_info_seq_show,
1439 static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
1441 struct acpi_video_bus *video = seq->private;
1443 unsigned long long id;
1449 status = acpi_video_bus_get_POST(video, &id);
1450 if (!ACPI_SUCCESS(status)) {
1451 seq_printf(seq, "<not supported>\n");
1454 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
1460 static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
1462 struct acpi_video_bus *video = seq->private;
1465 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
1470 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
1472 return single_open(file, acpi_video_bus_POST_seq_show,
1476 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
1478 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1482 acpi_video_bus_write_POST(struct file *file,
1483 const char __user * buffer,
1484 size_t count, loff_t * data)
1487 struct seq_file *m = file->private_data;
1488 struct acpi_video_bus *video = m->private;
1489 char str[12] = { 0 };
1490 unsigned long long opt, options;
1493 if (!video || count + 1 > sizeof str)
1496 status = acpi_video_bus_POST_options(video, &options);
1497 if (!ACPI_SUCCESS(status))
1500 if (copy_from_user(str, buffer, count))
1504 opt = strtoul(str, NULL, 0);
1508 /* just in case an OEM 'forgot' the motherboard... */
1511 if (options & (1ul << opt)) {
1512 status = acpi_video_bus_set_POST(video, opt);
1513 if (!ACPI_SUCCESS(status))
1522 acpi_video_bus_write_DOS(struct file *file,
1523 const char __user * buffer,
1524 size_t count, loff_t * data)
1527 struct seq_file *m = file->private_data;
1528 struct acpi_video_bus *video = m->private;
1529 char str[12] = { 0 };
1533 if (!video || count + 1 > sizeof str)
1536 if (copy_from_user(str, buffer, count))
1540 opt = strtoul(str, NULL, 0);
1544 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
1546 if (!ACPI_SUCCESS(status))
1552 static int acpi_video_bus_add_fs(struct acpi_device *device)
1554 struct acpi_video_bus *video = acpi_driver_data(device);
1555 struct proc_dir_entry *device_dir;
1556 struct proc_dir_entry *entry;
1558 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1563 entry = proc_create_data("info", S_IRUGO, device_dir,
1564 &acpi_video_bus_info_fops,
1565 acpi_driver_data(device));
1567 goto err_remove_dir;
1570 entry = proc_create_data("ROM", S_IRUGO, device_dir,
1571 &acpi_video_bus_ROM_fops,
1572 acpi_driver_data(device));
1574 goto err_remove_info;
1576 /* 'POST_info' [R] */
1577 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
1578 &acpi_video_bus_POST_info_fops,
1579 acpi_driver_data(device));
1581 goto err_remove_rom;
1584 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
1586 &acpi_video_bus_POST_fops,
1587 acpi_driver_data(device));
1589 goto err_remove_post_info;
1592 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
1594 &acpi_video_bus_DOS_fops,
1595 acpi_driver_data(device));
1597 goto err_remove_post;
1599 video->dir = acpi_device_dir(device) = device_dir;
1603 remove_proc_entry("POST", device_dir);
1604 err_remove_post_info:
1605 remove_proc_entry("POST_info", device_dir);
1607 remove_proc_entry("ROM", device_dir);
1609 remove_proc_entry("info", device_dir);
1611 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1615 static int acpi_video_bus_remove_fs(struct acpi_device *device)
1617 struct proc_dir_entry *device_dir = acpi_device_dir(device);
1620 remove_proc_entry("info", device_dir);
1621 remove_proc_entry("ROM", device_dir);
1622 remove_proc_entry("POST_info", device_dir);
1623 remove_proc_entry("POST", device_dir);
1624 remove_proc_entry("DOS", device_dir);
1625 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1626 acpi_device_dir(device) = NULL;
1632 /* --------------------------------------------------------------------------
1634 -------------------------------------------------------------------------- */
1636 /* device interface */
1637 static struct acpi_video_device_attrib*
1638 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1640 struct acpi_video_enumerated_device *ids;
1643 for (i = 0; i < video->attached_count; i++) {
1644 ids = &video->attached_array[i];
1645 if ((ids->value.int_val & 0xffff) == device_id)
1646 return &ids->value.attrib;
1653 acpi_video_bus_get_one_device(struct acpi_device *device,
1654 struct acpi_video_bus *video)
1656 unsigned long long device_id;
1658 struct acpi_video_device *data;
1659 struct acpi_video_device_attrib* attribute;
1661 if (!device || !video)
1665 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1666 if (ACPI_SUCCESS(status)) {
1668 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1672 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1673 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1674 device->driver_data = data;
1676 data->device_id = device_id;
1677 data->video = video;
1680 attribute = acpi_video_get_device_attr(video, device_id);
1682 if((attribute != NULL) && attribute->device_id_scheme) {
1683 switch (attribute->display_type) {
1684 case ACPI_VIDEO_DISPLAY_CRT:
1685 data->flags.crt = 1;
1687 case ACPI_VIDEO_DISPLAY_TV:
1688 data->flags.tvout = 1;
1690 case ACPI_VIDEO_DISPLAY_DVI:
1691 data->flags.dvi = 1;
1693 case ACPI_VIDEO_DISPLAY_LCD:
1694 data->flags.lcd = 1;
1697 data->flags.unknown = 1;
1700 if(attribute->bios_can_detect)
1701 data->flags.bios = 1;
1703 data->flags.unknown = 1;
1705 acpi_video_device_bind(video, data);
1706 acpi_video_device_find_cap(data);
1708 status = acpi_install_notify_handler(device->handle,
1710 acpi_video_device_notify,
1712 if (ACPI_FAILURE(status)) {
1713 printk(KERN_ERR PREFIX
1714 "Error installing notify handler\n");
1715 if(data->brightness)
1716 kfree(data->brightness->levels);
1717 kfree(data->brightness);
1722 mutex_lock(&video->device_list_lock);
1723 list_add_tail(&data->entry, &video->video_device_list);
1724 mutex_unlock(&video->device_list_lock);
1726 acpi_video_device_add_fs(device);
1736 * video : video bus device
1741 * Enumerate the video device list of the video bus,
1742 * bind the ids with the corresponding video devices
1743 * under the video bus.
1746 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1748 struct acpi_video_device *dev;
1750 mutex_lock(&video->device_list_lock);
1752 list_for_each_entry(dev, &video->video_device_list, entry)
1753 acpi_video_device_bind(video, dev);
1755 mutex_unlock(&video->device_list_lock);
1760 * video : video bus device
1761 * device : video output device under the video
1767 * Bind the ids with the corresponding video devices
1768 * under the video bus.
1772 acpi_video_device_bind(struct acpi_video_bus *video,
1773 struct acpi_video_device *device)
1775 struct acpi_video_enumerated_device *ids;
1778 for (i = 0; i < video->attached_count; i++) {
1779 ids = &video->attached_array[i];
1780 if (device->device_id == (ids->value.int_val & 0xffff)) {
1781 ids->bind_info = device;
1782 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1789 * video : video bus device
1794 * Call _DOD to enumerate all devices attached to display adapter
1798 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1803 struct acpi_video_enumerated_device *active_list;
1804 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1805 union acpi_object *dod = NULL;
1806 union acpi_object *obj;
1808 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1809 if (!ACPI_SUCCESS(status)) {
1810 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1814 dod = buffer.pointer;
1815 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1816 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1821 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1822 dod->package.count));
1824 active_list = kcalloc(1 + dod->package.count,
1825 sizeof(struct acpi_video_enumerated_device),
1833 for (i = 0; i < dod->package.count; i++) {
1834 obj = &dod->package.elements[i];
1836 if (obj->type != ACPI_TYPE_INTEGER) {
1837 printk(KERN_ERR PREFIX
1838 "Invalid _DOD data in element %d\n", i);
1842 active_list[count].value.int_val = obj->integer.value;
1843 active_list[count].bind_info = NULL;
1844 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1845 (int)obj->integer.value));
1849 kfree(video->attached_array);
1851 video->attached_array = active_list;
1852 video->attached_count = count;
1855 kfree(buffer.pointer);
1860 acpi_video_get_next_level(struct acpi_video_device *device,
1861 u32 level_current, u32 event)
1863 int min, max, min_above, max_below, i, l, delta = 255;
1864 max = max_below = 0;
1865 min = min_above = 255;
1866 /* Find closest level to level_current */
1867 for (i = 2; i < device->brightness->count; i++) {
1868 l = device->brightness->levels[i];
1869 if (abs(l - level_current) < abs(delta)) {
1870 delta = l - level_current;
1875 /* Ajust level_current to closest available level */
1876 level_current += delta;
1877 for (i = 2; i < device->brightness->count; i++) {
1878 l = device->brightness->levels[i];
1883 if (l < min_above && l > level_current)
1885 if (l > max_below && l < level_current)
1890 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1891 return (level_current < max) ? min_above : min;
1892 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1893 return (level_current < max) ? min_above : max;
1894 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1895 return (level_current > min) ? max_below : min;
1896 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1897 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1900 return level_current;
1905 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1907 unsigned long long level_current, level_next;
1908 int result = -EINVAL;
1910 if (!device->brightness)
1913 result = acpi_video_device_lcd_get_level_current(device,
1918 level_next = acpi_video_get_next_level(device, level_current, event);
1920 result = acpi_video_device_lcd_set_level(device, level_next);
1924 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1930 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1931 struct acpi_device *device)
1934 struct acpi_device *dev;
1936 acpi_video_device_enumerate(video);
1938 list_for_each_entry(dev, &device->children, node) {
1940 status = acpi_video_bus_get_one_device(dev, video);
1941 if (ACPI_FAILURE(status)) {
1942 printk(KERN_WARNING PREFIX
1943 "Cant attach device");
1950 static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1953 struct acpi_video_bus *video;
1956 if (!device || !device->video)
1959 video = device->video;
1961 acpi_video_device_remove_fs(device->dev);
1963 status = acpi_remove_notify_handler(device->dev->handle,
1965 acpi_video_device_notify);
1966 backlight_device_unregister(device->backlight);
1968 sysfs_remove_link(&device->dev->dev.kobj,
1970 sysfs_remove_link(&device->cdev->device.kobj,
1972 thermal_cooling_device_unregister(device->cdev);
1973 device->cdev = NULL;
1975 video_output_unregister(device->output_dev);
1980 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1983 struct acpi_video_device *dev, *next;
1985 mutex_lock(&video->device_list_lock);
1987 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1989 status = acpi_video_bus_put_one_device(dev);
1990 if (ACPI_FAILURE(status))
1991 printk(KERN_WARNING PREFIX
1992 "hhuuhhuu bug in acpi video driver.\n");
1994 if (dev->brightness) {
1995 kfree(dev->brightness->levels);
1996 kfree(dev->brightness);
1998 list_del(&dev->entry);
2002 mutex_unlock(&video->device_list_lock);
2007 /* acpi_video interface */
2009 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
2011 return acpi_video_bus_DOS(video, 0, 0);
2014 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
2016 return acpi_video_bus_DOS(video, 0, 1);
2019 static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
2021 struct acpi_video_bus *video = acpi_driver_data(device);
2022 struct input_dev *input;
2028 input = video->input;
2031 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
2032 * most likely via hotkey. */
2033 acpi_bus_generate_proc_event(device, event, 0);
2034 keycode = KEY_SWITCHVIDEOMODE;
2037 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
2039 acpi_video_device_enumerate(video);
2040 acpi_video_device_rebind(video);
2041 acpi_bus_generate_proc_event(device, event, 0);
2042 keycode = KEY_SWITCHVIDEOMODE;
2045 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
2046 acpi_bus_generate_proc_event(device, event, 0);
2047 keycode = KEY_SWITCHVIDEOMODE;
2049 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
2050 acpi_bus_generate_proc_event(device, event, 0);
2051 keycode = KEY_VIDEO_NEXT;
2053 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
2054 acpi_bus_generate_proc_event(device, event, 0);
2055 keycode = KEY_VIDEO_PREV;
2059 keycode = KEY_UNKNOWN;
2060 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2061 "Unsupported event [0x%x]\n", event));
2065 acpi_notifier_call_chain(device, event, 0);
2066 input_report_key(input, keycode, 1);
2068 input_report_key(input, keycode, 0);
2074 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
2076 struct acpi_video_device *video_device = data;
2077 struct acpi_device *device = NULL;
2078 struct acpi_video_bus *bus;
2079 struct input_dev *input;
2085 device = video_device->dev;
2086 bus = video_device->video;
2090 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
2091 if (brightness_switch_enabled)
2092 acpi_video_switch_brightness(video_device, event);
2093 acpi_bus_generate_proc_event(device, event, 0);
2094 keycode = KEY_BRIGHTNESS_CYCLE;
2096 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
2097 if (brightness_switch_enabled)
2098 acpi_video_switch_brightness(video_device, event);
2099 acpi_bus_generate_proc_event(device, event, 0);
2100 keycode = KEY_BRIGHTNESSUP;
2102 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
2103 if (brightness_switch_enabled)
2104 acpi_video_switch_brightness(video_device, event);
2105 acpi_bus_generate_proc_event(device, event, 0);
2106 keycode = KEY_BRIGHTNESSDOWN;
2108 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
2109 if (brightness_switch_enabled)
2110 acpi_video_switch_brightness(video_device, event);
2111 acpi_bus_generate_proc_event(device, event, 0);
2112 keycode = KEY_BRIGHTNESS_ZERO;
2114 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
2115 if (brightness_switch_enabled)
2116 acpi_video_switch_brightness(video_device, event);
2117 acpi_bus_generate_proc_event(device, event, 0);
2118 keycode = KEY_DISPLAY_OFF;
2121 keycode = KEY_UNKNOWN;
2122 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2123 "Unsupported event [0x%x]\n", event));
2127 acpi_notifier_call_chain(device, event, 0);
2128 input_report_key(input, keycode, 1);
2130 input_report_key(input, keycode, 0);
2136 static int instance;
2137 static int acpi_video_resume(struct acpi_device *device)
2139 struct acpi_video_bus *video;
2140 struct acpi_video_device *video_device;
2143 if (!device || !acpi_driver_data(device))
2146 video = acpi_driver_data(device);
2148 for (i = 0; i < video->attached_count; i++) {
2149 video_device = video->attached_array[i].bind_info;
2150 if (video_device && video_device->backlight)
2151 acpi_video_set_brightness(video_device->backlight);
2156 static int acpi_video_bus_add(struct acpi_device *device)
2158 struct acpi_video_bus *video;
2159 struct input_dev *input;
2162 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
2166 /* a hack to fix the duplicate name "VID" problem on T61 */
2167 if (!strcmp(device->pnp.bus_id, "VID")) {
2169 device->pnp.bus_id[3] = '0' + instance;
2172 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2173 if (!strcmp(device->pnp.bus_id, "VGA")) {
2175 device->pnp.bus_id[3] = '0' + instance;
2179 video->device = device;
2180 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2181 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
2182 device->driver_data = video;
2184 acpi_video_bus_find_cap(video);
2185 error = acpi_video_bus_check(video);
2187 goto err_free_video;
2189 error = acpi_video_bus_add_fs(device);
2191 goto err_free_video;
2193 mutex_init(&video->device_list_lock);
2194 INIT_LIST_HEAD(&video->video_device_list);
2196 acpi_video_bus_get_devices(video, device);
2197 acpi_video_bus_start_devices(video);
2199 video->input = input = input_allocate_device();
2202 goto err_stop_video;
2205 snprintf(video->phys, sizeof(video->phys),
2206 "%s/video/input0", acpi_device_hid(video->device));
2208 input->name = acpi_device_name(video->device);
2209 input->phys = video->phys;
2210 input->id.bustype = BUS_HOST;
2211 input->id.product = 0x06;
2212 input->dev.parent = &device->dev;
2213 input->evbit[0] = BIT(EV_KEY);
2214 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2215 set_bit(KEY_VIDEO_NEXT, input->keybit);
2216 set_bit(KEY_VIDEO_PREV, input->keybit);
2217 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2218 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2219 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2220 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2221 set_bit(KEY_DISPLAY_OFF, input->keybit);
2222 set_bit(KEY_UNKNOWN, input->keybit);
2224 error = input_register_device(input);
2226 goto err_free_input_dev;
2228 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
2229 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2230 video->flags.multihead ? "yes" : "no",
2231 video->flags.rom ? "yes" : "no",
2232 video->flags.post ? "yes" : "no");
2237 input_free_device(input);
2239 acpi_video_bus_stop_devices(video);
2240 acpi_video_bus_put_devices(video);
2241 kfree(video->attached_array);
2242 acpi_video_bus_remove_fs(device);
2245 device->driver_data = NULL;
2250 static int acpi_video_bus_remove(struct acpi_device *device, int type)
2252 struct acpi_video_bus *video = NULL;
2255 if (!device || !acpi_driver_data(device))
2258 video = acpi_driver_data(device);
2260 acpi_video_bus_stop_devices(video);
2261 acpi_video_bus_put_devices(video);
2262 acpi_video_bus_remove_fs(device);
2264 input_unregister_device(video->input);
2265 kfree(video->attached_array);
2271 static int __init intel_opregion_present(void)
2273 #if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2274 struct pci_dev *dev = NULL;
2277 for_each_pci_dev(dev) {
2278 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2280 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2282 pci_read_config_dword(dev, 0xfc, &address);
2291 int acpi_video_register(void)
2295 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2296 if (!acpi_video_dir)
2299 result = acpi_bus_register_driver(&acpi_video_bus);
2301 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2307 EXPORT_SYMBOL(acpi_video_register);
2310 * This is kind of nasty. Hardware using Intel chipsets may require
2311 * the video opregion code to be run first in order to initialise
2312 * state before any ACPI video calls are made. To handle this we defer
2313 * registration of the video class until the opregion code has run.
2316 static int __init acpi_video_init(void)
2318 dmi_check_system(video_dmi_table);
2320 if (intel_opregion_present())
2323 return acpi_video_register();
2326 static void __exit acpi_video_exit(void)
2329 acpi_bus_unregister_driver(&acpi_video_bus);
2331 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2336 module_init(acpi_video_init);
2337 module_exit(acpi_video_exit);