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);
81 static int register_count = 0;
82 static int acpi_video_bus_add(struct acpi_device *device);
83 static int acpi_video_bus_remove(struct acpi_device *device, int type);
84 static int acpi_video_resume(struct acpi_device *device);
85 static void acpi_video_bus_notify(struct acpi_device *device, u32 event);
87 static const struct acpi_device_id video_device_ids[] = {
91 MODULE_DEVICE_TABLE(acpi, video_device_ids);
93 static struct acpi_driver acpi_video_bus = {
95 .class = ACPI_VIDEO_CLASS,
96 .ids = video_device_ids,
98 .add = acpi_video_bus_add,
99 .remove = acpi_video_bus_remove,
100 .resume = acpi_video_resume,
101 .notify = acpi_video_bus_notify,
105 struct acpi_video_bus_flags {
106 u8 multihead:1; /* can switch video heads */
107 u8 rom:1; /* can retrieve a video rom */
108 u8 post:1; /* can configure the head to */
112 struct acpi_video_bus_cap {
113 u8 _DOS:1; /*Enable/Disable output switching */
114 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
115 u8 _ROM:1; /*Get ROM Data */
116 u8 _GPD:1; /*Get POST Device */
117 u8 _SPD:1; /*Set POST Device */
118 u8 _VPO:1; /*Video POST Options */
122 struct acpi_video_device_attrib {
123 u32 display_index:4; /* A zero-based instance of the Display */
124 u32 display_port_attachment:4; /*This field differentiates the display type */
125 u32 display_type:4; /*Describe the specific type in use */
126 u32 vendor_specific:4; /*Chipset Vendor Specific */
127 u32 bios_can_detect:1; /*BIOS can detect the device */
128 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
130 u32 pipe_id:3; /*For VGA multiple-head devices. */
131 u32 reserved:10; /*Must be 0 */
132 u32 device_id_scheme:1; /*Device ID Scheme */
135 struct acpi_video_enumerated_device {
138 struct acpi_video_device_attrib attrib;
140 struct acpi_video_device *bind_info;
143 struct acpi_video_bus {
144 struct acpi_device *device;
146 struct acpi_video_enumerated_device *attached_array;
148 struct acpi_video_bus_cap cap;
149 struct acpi_video_bus_flags flags;
150 struct list_head video_device_list;
151 struct mutex device_list_lock; /* protects video_device_list */
152 struct proc_dir_entry *dir;
153 struct input_dev *input;
154 char phys[32]; /* for input device */
157 struct acpi_video_device_flags {
167 struct acpi_video_device_cap {
168 u8 _ADR:1; /*Return the unique ID */
169 u8 _BCL:1; /*Query list of brightness control levels supported */
170 u8 _BCM:1; /*Set the brightness level */
171 u8 _BQC:1; /* Get current brightness level */
172 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
173 u8 _DDC:1; /*Return the EDID for this device */
174 u8 _DCS:1; /*Return status of output device */
175 u8 _DGS:1; /*Query graphics state */
176 u8 _DSS:1; /*Device state set */
179 struct acpi_video_brightness_flags {
180 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
181 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
182 u8 _BCL_use_index:1; /* levels in _BCL are index values */
183 u8 _BCM_use_index:1; /* input of _BCM is an index value */
184 u8 _BQC_use_index:1; /* _BQC returns an index value */
187 struct acpi_video_device_brightness {
191 struct acpi_video_brightness_flags flags;
194 struct acpi_video_device {
195 unsigned long device_id;
196 struct acpi_video_device_flags flags;
197 struct acpi_video_device_cap cap;
198 struct list_head entry;
199 struct acpi_video_bus *video;
200 struct acpi_device *dev;
201 struct acpi_video_device_brightness *brightness;
202 struct backlight_device *backlight;
203 struct thermal_cooling_device *cooling_dev;
204 struct output_device *output_dev;
208 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
209 static const struct file_operations acpi_video_bus_info_fops = {
210 .owner = THIS_MODULE,
211 .open = acpi_video_bus_info_open_fs,
214 .release = single_release,
217 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
218 static const struct file_operations acpi_video_bus_ROM_fops = {
219 .owner = THIS_MODULE,
220 .open = acpi_video_bus_ROM_open_fs,
223 .release = single_release,
226 static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
228 static const struct file_operations acpi_video_bus_POST_info_fops = {
229 .owner = THIS_MODULE,
230 .open = acpi_video_bus_POST_info_open_fs,
233 .release = single_release,
236 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
237 static ssize_t acpi_video_bus_write_POST(struct file *file,
238 const char __user *buffer, size_t count, loff_t *data);
239 static const struct file_operations acpi_video_bus_POST_fops = {
240 .owner = THIS_MODULE,
241 .open = acpi_video_bus_POST_open_fs,
243 .write = acpi_video_bus_write_POST,
245 .release = single_release,
248 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
249 static ssize_t acpi_video_bus_write_DOS(struct file *file,
250 const char __user *buffer, size_t count, loff_t *data);
251 static const struct file_operations acpi_video_bus_DOS_fops = {
252 .owner = THIS_MODULE,
253 .open = acpi_video_bus_DOS_open_fs,
255 .write = acpi_video_bus_write_DOS,
257 .release = single_release,
261 static int acpi_video_device_info_open_fs(struct inode *inode,
263 static const struct file_operations acpi_video_device_info_fops = {
264 .owner = THIS_MODULE,
265 .open = acpi_video_device_info_open_fs,
268 .release = single_release,
271 static int acpi_video_device_state_open_fs(struct inode *inode,
273 static ssize_t acpi_video_device_write_state(struct file *file,
274 const char __user *buffer, size_t count, loff_t *data);
275 static const struct file_operations acpi_video_device_state_fops = {
276 .owner = THIS_MODULE,
277 .open = acpi_video_device_state_open_fs,
279 .write = acpi_video_device_write_state,
281 .release = single_release,
284 static int acpi_video_device_brightness_open_fs(struct inode *inode,
286 static ssize_t acpi_video_device_write_brightness(struct file *file,
287 const char __user *buffer, size_t count, loff_t *data);
288 static const struct file_operations acpi_video_device_brightness_fops = {
289 .owner = THIS_MODULE,
290 .open = acpi_video_device_brightness_open_fs,
292 .write = acpi_video_device_write_brightness,
294 .release = single_release,
297 static int acpi_video_device_EDID_open_fs(struct inode *inode,
299 static const struct file_operations acpi_video_device_EDID_fops = {
300 .owner = THIS_MODULE,
301 .open = acpi_video_device_EDID_open_fs,
304 .release = single_release,
307 static const char device_decode[][30] = {
308 "motherboard VGA device",
314 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
315 static void acpi_video_device_rebind(struct acpi_video_bus *video);
316 static void acpi_video_device_bind(struct acpi_video_bus *video,
317 struct acpi_video_device *device);
318 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
319 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
321 static int acpi_video_device_lcd_get_level_current(
322 struct acpi_video_device *device,
323 unsigned long long *level);
324 static int acpi_video_get_next_level(struct acpi_video_device *device,
325 u32 level_current, u32 event);
326 static int acpi_video_switch_brightness(struct acpi_video_device *device,
328 static int acpi_video_device_get_state(struct acpi_video_device *device,
329 unsigned long long *state);
330 static int acpi_video_output_get(struct output_device *od);
331 static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
333 /*backlight device sysfs support*/
334 static int acpi_video_get_brightness(struct backlight_device *bd)
336 unsigned long long cur_level;
338 struct acpi_video_device *vd =
339 (struct acpi_video_device *)bl_get_data(bd);
341 if (acpi_video_device_lcd_get_level_current(vd, &cur_level))
343 for (i = 2; i < vd->brightness->count; i++) {
344 if (vd->brightness->levels[i] == cur_level)
345 /* The first two entries are special - see page 575
346 of the ACPI spec 3.0 */
352 static int acpi_video_set_brightness(struct backlight_device *bd)
354 int request_level = bd->props.brightness + 2;
355 struct acpi_video_device *vd =
356 (struct acpi_video_device *)bl_get_data(bd);
358 return acpi_video_device_lcd_set_level(vd,
359 vd->brightness->levels[request_level]);
362 static struct backlight_ops acpi_backlight_ops = {
363 .get_brightness = acpi_video_get_brightness,
364 .update_status = acpi_video_set_brightness,
367 /*video output device sysfs support*/
368 static int acpi_video_output_get(struct output_device *od)
370 unsigned long long state;
371 struct acpi_video_device *vd =
372 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
373 acpi_video_device_get_state(vd, &state);
377 static int acpi_video_output_set(struct output_device *od)
379 unsigned long state = od->request_state;
380 struct acpi_video_device *vd=
381 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
382 return acpi_video_device_set_state(vd, state);
385 static struct output_properties acpi_output_properties = {
386 .set_state = acpi_video_output_set,
387 .get_status = acpi_video_output_get,
391 /* thermal cooling device callbacks */
392 static int video_get_max_state(struct thermal_cooling_device *cooling_dev, unsigned
395 struct acpi_device *device = cooling_dev->devdata;
396 struct acpi_video_device *video = acpi_driver_data(device);
398 *state = video->brightness->count - 3;
402 static int video_get_cur_state(struct thermal_cooling_device *cooling_dev, unsigned
405 struct acpi_device *device = cooling_dev->devdata;
406 struct acpi_video_device *video = acpi_driver_data(device);
407 unsigned long long level;
410 if (acpi_video_device_lcd_get_level_current(video, &level))
412 for (offset = 2; offset < video->brightness->count; offset++)
413 if (level == video->brightness->levels[offset]) {
414 *state = video->brightness->count - offset - 1;
422 video_set_cur_state(struct thermal_cooling_device *cooling_dev, unsigned long state)
424 struct acpi_device *device = cooling_dev->devdata;
425 struct acpi_video_device *video = acpi_driver_data(device);
428 if ( state >= video->brightness->count - 2)
431 state = video->brightness->count - state;
432 level = video->brightness->levels[state -1];
433 return acpi_video_device_lcd_set_level(video, level);
436 static struct thermal_cooling_device_ops video_cooling_ops = {
437 .get_max_state = video_get_max_state,
438 .get_cur_state = video_get_cur_state,
439 .set_cur_state = video_set_cur_state,
442 /* --------------------------------------------------------------------------
444 -------------------------------------------------------------------------- */
449 acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
453 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
459 acpi_video_device_get_state(struct acpi_video_device *device,
460 unsigned long long *state)
464 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
470 acpi_video_device_set_state(struct acpi_video_device *device, int state)
473 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
474 struct acpi_object_list args = { 1, &arg0 };
475 unsigned long long ret;
478 arg0.integer.value = state;
479 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
485 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
486 union acpi_object **levels)
489 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
490 union acpi_object *obj;
495 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
496 if (!ACPI_SUCCESS(status))
498 obj = (union acpi_object *)buffer.pointer;
499 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
500 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
510 kfree(buffer.pointer);
516 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
519 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
520 struct acpi_object_list args = { 1, &arg0 };
523 arg0.integer.value = level;
525 status = acpi_evaluate_object(device->dev->handle, "_BCM",
527 if (ACPI_FAILURE(status)) {
528 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
532 device->brightness->curr = level;
533 for (state = 2; state < device->brightness->count; state++)
534 if (level == device->brightness->levels[state]) {
535 if (device->backlight)
536 device->backlight->props.brightness = state - 2;
540 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
545 * For some buggy _BQC methods, we need to add a constant value to
546 * the _BQC return value to get the actual current brightness level
549 static int bqc_offset_aml_bug_workaround;
550 static int __init video_set_bqc_offset(const struct dmi_system_id *d)
552 bqc_offset_aml_bug_workaround = 9;
556 static struct dmi_system_id video_dmi_table[] __initdata = {
558 * Broken _BQC workaround http://bugzilla.kernel.org/show_bug.cgi?id=13121
561 .callback = video_set_bqc_offset,
562 .ident = "Acer Aspire 5720",
564 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
565 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5720"),
569 .callback = video_set_bqc_offset,
570 .ident = "Acer Aspire 5710Z",
572 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
573 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5710Z"),
577 .callback = video_set_bqc_offset,
578 .ident = "eMachines E510",
580 DMI_MATCH(DMI_BOARD_VENDOR, "EMACHINES"),
581 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines E510"),
585 .callback = video_set_bqc_offset,
586 .ident = "Acer Aspire 5315",
588 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
589 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 5315"),
593 .callback = video_set_bqc_offset,
594 .ident = "Acer Aspire 7720",
596 DMI_MATCH(DMI_BOARD_VENDOR, "Acer"),
597 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 7720"),
604 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
605 unsigned long long *level)
607 acpi_status status = AE_OK;
610 if (device->cap._BQC || device->cap._BCQ) {
611 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
613 status = acpi_evaluate_integer(device->dev->handle, buf,
615 if (ACPI_SUCCESS(status)) {
616 if (device->brightness->flags._BQC_use_index) {
617 if (device->brightness->flags._BCL_reversed)
618 *level = device->brightness->count
620 *level = device->brightness->levels[*level + 2];
623 *level += bqc_offset_aml_bug_workaround;
624 for (i = 2; i < device->brightness->count; i++)
625 if (device->brightness->levels[i] == *level) {
626 device->brightness->curr = *level;
629 /* BQC returned an invalid level. Stop using it. */
630 ACPI_WARNING((AE_INFO, "%s returned an invalid level",
632 device->cap._BQC = device->cap._BCQ = 0;
635 * should we return an error or ignore this failure?
636 * dev->brightness->curr is a cached value which stores
637 * the correct current backlight level in most cases.
638 * ACPI video backlight still works w/ buggy _BQC.
639 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
641 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
642 device->cap._BQC = device->cap._BCQ = 0;
646 *level = device->brightness->curr;
651 acpi_video_device_EDID(struct acpi_video_device *device,
652 union acpi_object **edid, ssize_t length)
655 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
656 union acpi_object *obj;
657 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
658 struct acpi_object_list args = { 1, &arg0 };
666 arg0.integer.value = 1;
667 else if (length == 256)
668 arg0.integer.value = 2;
672 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
673 if (ACPI_FAILURE(status))
676 obj = buffer.pointer;
678 if (obj && obj->type == ACPI_TYPE_BUFFER)
681 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
692 acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
695 unsigned long long tmp;
696 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
697 struct acpi_object_list args = { 1, &arg0 };
700 arg0.integer.value = option;
702 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
703 if (ACPI_SUCCESS(status))
704 status = tmp ? (-EINVAL) : (AE_OK);
710 acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
714 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
720 acpi_video_bus_POST_options(struct acpi_video_bus *video,
721 unsigned long long *options)
725 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
733 * video : video bus device pointer
735 * 0. The system BIOS should NOT automatically switch(toggle)
736 * the active display output.
737 * 1. The system BIOS should automatically switch (toggle) the
738 * active display output. No switch event.
739 * 2. The _DGS value should be locked.
740 * 3. The system BIOS should not automatically switch (toggle) the
741 * active display output, but instead generate the display switch
744 * 0. The system BIOS should automatically control the brightness level
745 * of the LCD when the power changes from AC to DC
746 * 1. The system BIOS should NOT automatically control the brightness
747 * level of the LCD when the power changes from AC to DC.
753 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
755 acpi_integer status = 0;
756 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
757 struct acpi_object_list args = { 1, &arg0 };
760 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
764 arg0.integer.value = (lcd_flag << 2) | bios_flag;
765 video->dos_setting = arg0.integer.value;
766 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
773 * Simple comparison function used to sort backlight levels.
777 acpi_video_cmp_level(const void *a, const void *b)
779 return *(int *)a - *(int *)b;
784 * device : video output device (LCD, CRT, ..)
787 * Maximum brightness level
789 * Allocate and initialize device->brightness.
793 acpi_video_init_brightness(struct acpi_video_device *device)
795 union acpi_object *obj = NULL;
796 int i, max_level = 0, count = 0, level_ac_battery = 0;
797 unsigned long long level, level_old;
798 union acpi_object *o;
799 struct acpi_video_device_brightness *br = NULL;
800 int result = -EINVAL;
802 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
803 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
804 "LCD brightness level\n"));
808 if (obj->package.count < 2)
811 br = kzalloc(sizeof(*br), GFP_KERNEL);
813 printk(KERN_ERR "can't allocate memory\n");
818 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
825 for (i = 0; i < obj->package.count; i++) {
826 o = (union acpi_object *)&obj->package.elements[i];
827 if (o->type != ACPI_TYPE_INTEGER) {
828 printk(KERN_ERR PREFIX "Invalid data\n");
831 br->levels[count] = (u32) o->integer.value;
833 if (br->levels[count] > max_level)
834 max_level = br->levels[count];
839 * some buggy BIOS don't export the levels
840 * when machine is on AC/Battery in _BCL package.
841 * In this case, the first two elements in _BCL packages
842 * are also supported brightness levels that OS should take care of.
844 for (i = 2; i < count; i++) {
845 if (br->levels[i] == br->levels[0])
847 if (br->levels[i] == br->levels[1])
851 if (level_ac_battery < 2) {
852 level_ac_battery = 2 - level_ac_battery;
853 br->flags._BCL_no_ac_battery_levels = 1;
854 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
855 br->levels[i] = br->levels[i - level_ac_battery];
856 count += level_ac_battery;
857 } else if (level_ac_battery > 2)
858 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
860 /* Check if the _BCL package is in a reversed order */
861 if (max_level == br->levels[2]) {
862 br->flags._BCL_reversed = 1;
863 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
864 acpi_video_cmp_level, NULL);
865 } else if (max_level != br->levels[count - 1])
867 "Found unordered _BCL package\n"));
870 device->brightness = br;
872 /* Check the input/output of _BQC/_BCL/_BCM */
873 if ((max_level < 100) && (max_level <= (count - 2)))
874 br->flags._BCL_use_index = 1;
877 * _BCM is always consistent with _BCL,
878 * at least for all the laptops we have ever seen.
880 br->flags._BCM_use_index = br->flags._BCL_use_index;
882 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
883 br->curr = level = max_level;
885 if (!device->cap._BQC)
888 result = acpi_video_device_lcd_get_level_current(device, &level_old);
890 goto out_free_levels;
893 * Set the level to maximum and check if _BQC uses indexed value
895 result = acpi_video_device_lcd_set_level(device, max_level);
897 goto out_free_levels;
899 result = acpi_video_device_lcd_get_level_current(device, &level);
901 goto out_free_levels;
903 br->flags._BQC_use_index = (level == max_level ? 0 : 1);
905 if (!br->flags._BQC_use_index) {
907 * Set the backlight to the initial state.
908 * On some buggy laptops, _BQC returns an uninitialized value
909 * when invoked for the first time, i.e. level_old is invalid.
910 * set the backlight to max_level in this case
912 for (i = 2; i < br->count; i++)
913 if (level_old == br->levels[i])
918 if (br->flags._BCL_reversed)
919 level_old = (br->count - 1) - level_old;
920 level = br->levels[level_old];
923 result = acpi_video_device_lcd_set_level(device, level);
925 goto out_free_levels;
927 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
928 "found %d brightness levels\n", count - 2));
937 device->brightness = NULL;
944 * device : video output device (LCD, CRT, ..)
949 * Find out all required AML methods defined under the output
953 static void acpi_video_device_find_cap(struct acpi_video_device *device)
955 acpi_handle h_dummy1;
957 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
958 device->cap._ADR = 1;
960 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
961 device->cap._BCL = 1;
963 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
964 device->cap._BCM = 1;
966 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
967 device->cap._BQC = 1;
968 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
970 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
971 device->cap._BCQ = 1;
974 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
975 device->cap._DDC = 1;
977 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
978 device->cap._DCS = 1;
980 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
981 device->cap._DGS = 1;
983 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
984 device->cap._DSS = 1;
987 if (acpi_video_backlight_support()) {
989 static int count = 0;
992 result = acpi_video_init_brightness(device);
995 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
999 sprintf(name, "acpi_video%d", count++);
1000 device->backlight = backlight_device_register(name,
1001 NULL, device, &acpi_backlight_ops);
1003 if (IS_ERR(device->backlight))
1005 device->backlight->props.max_brightness = device->brightness->count-3;
1007 result = sysfs_create_link(&device->backlight->dev.kobj,
1008 &device->dev->dev.kobj, "device");
1010 printk(KERN_ERR PREFIX "Create sysfs link\n");
1012 device->cooling_dev = thermal_cooling_device_register("LCD",
1013 device->dev, &video_cooling_ops);
1014 if (IS_ERR(device->cooling_dev)) {
1016 * Set cooling_dev to NULL so we don't crash trying to
1018 * Also, why the hell we are returning early and
1019 * not attempt to register video output if cooling
1020 * device registration failed?
1023 device->cooling_dev = NULL;
1027 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
1028 device->cooling_dev->id);
1029 result = sysfs_create_link(&device->dev->dev.kobj,
1030 &device->cooling_dev->device.kobj,
1033 printk(KERN_ERR PREFIX "Create sysfs link\n");
1034 result = sysfs_create_link(&device->cooling_dev->device.kobj,
1035 &device->dev->dev.kobj, "device");
1037 printk(KERN_ERR PREFIX "Create sysfs link\n");
1041 if (acpi_video_display_switch_support()) {
1043 if (device->cap._DCS && device->cap._DSS) {
1046 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
1049 sprintf(name, "acpi_video%d", count++);
1050 device->output_dev = video_output_register(name,
1051 NULL, device, &acpi_output_properties);
1059 * device : video output device (VGA)
1064 * Find out all required AML methods defined under the video bus device.
1067 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
1069 acpi_handle h_dummy1;
1071 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
1072 video->cap._DOS = 1;
1074 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
1075 video->cap._DOD = 1;
1077 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
1078 video->cap._ROM = 1;
1080 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
1081 video->cap._GPD = 1;
1083 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
1084 video->cap._SPD = 1;
1086 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
1087 video->cap._VPO = 1;
1092 * Check whether the video bus device has required AML method to
1093 * support the desired features
1096 static int acpi_video_bus_check(struct acpi_video_bus *video)
1098 acpi_status status = -ENOENT;
1099 struct pci_dev *dev;
1104 dev = acpi_get_pci_dev(video->device->handle);
1109 /* Since there is no HID, CID and so on for VGA driver, we have
1110 * to check well known required nodes.
1113 /* Does this device support video switching? */
1114 if (video->cap._DOS || video->cap._DOD) {
1115 if (!video->cap._DOS) {
1116 printk(KERN_WARNING FW_BUG
1117 "ACPI(%s) defines _DOD but not _DOS\n",
1118 acpi_device_bid(video->device));
1120 video->flags.multihead = 1;
1124 /* Does this device support retrieving a video ROM? */
1125 if (video->cap._ROM) {
1126 video->flags.rom = 1;
1130 /* Does this device support configuring which video device to POST? */
1131 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
1132 video->flags.post = 1;
1139 /* --------------------------------------------------------------------------
1140 FS Interface (/proc)
1141 -------------------------------------------------------------------------- */
1143 static struct proc_dir_entry *acpi_video_dir;
1147 static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
1149 struct acpi_video_device *dev = seq->private;
1155 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1156 seq_printf(seq, "type: ");
1158 seq_printf(seq, "CRT\n");
1159 else if (dev->flags.lcd)
1160 seq_printf(seq, "LCD\n");
1161 else if (dev->flags.tvout)
1162 seq_printf(seq, "TVOUT\n");
1163 else if (dev->flags.dvi)
1164 seq_printf(seq, "DVI\n");
1166 seq_printf(seq, "UNKNOWN\n");
1168 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
1175 acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
1177 return single_open(file, acpi_video_device_info_seq_show,
1181 static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
1184 struct acpi_video_device *dev = seq->private;
1185 unsigned long long state;
1191 status = acpi_video_device_get_state(dev, &state);
1192 seq_printf(seq, "state: ");
1193 if (ACPI_SUCCESS(status))
1194 seq_printf(seq, "0x%02llx\n", state);
1196 seq_printf(seq, "<not supported>\n");
1198 status = acpi_video_device_query(dev, &state);
1199 seq_printf(seq, "query: ");
1200 if (ACPI_SUCCESS(status))
1201 seq_printf(seq, "0x%02llx\n", state);
1203 seq_printf(seq, "<not supported>\n");
1210 acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
1212 return single_open(file, acpi_video_device_state_seq_show,
1217 acpi_video_device_write_state(struct file *file,
1218 const char __user * buffer,
1219 size_t count, loff_t * data)
1222 struct seq_file *m = file->private_data;
1223 struct acpi_video_device *dev = m->private;
1224 char str[12] = { 0 };
1228 if (!dev || count >= sizeof(str))
1231 if (copy_from_user(str, buffer, count))
1235 state = simple_strtoul(str, NULL, 0);
1236 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
1238 status = acpi_video_device_set_state(dev, state);
1247 acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
1249 struct acpi_video_device *dev = seq->private;
1253 if (!dev || !dev->brightness) {
1254 seq_printf(seq, "<not supported>\n");
1258 seq_printf(seq, "levels: ");
1259 for (i = 2; i < dev->brightness->count; i++)
1260 seq_printf(seq, " %d", dev->brightness->levels[i]);
1261 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1267 acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
1269 return single_open(file, acpi_video_device_brightness_seq_show,
1274 acpi_video_device_write_brightness(struct file *file,
1275 const char __user * buffer,
1276 size_t count, loff_t * data)
1278 struct seq_file *m = file->private_data;
1279 struct acpi_video_device *dev = m->private;
1280 char str[5] = { 0 };
1281 unsigned int level = 0;
1285 if (!dev || !dev->brightness || count >= sizeof(str))
1288 if (copy_from_user(str, buffer, count))
1292 level = simple_strtoul(str, NULL, 0);
1297 /* validate through the list of available levels */
1298 for (i = 2; i < dev->brightness->count; i++)
1299 if (level == dev->brightness->levels[i]) {
1300 if (!acpi_video_device_lcd_set_level(dev, level))
1308 static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
1310 struct acpi_video_device *dev = seq->private;
1313 union acpi_object *edid = NULL;
1319 status = acpi_video_device_EDID(dev, &edid, 128);
1320 if (ACPI_FAILURE(status)) {
1321 status = acpi_video_device_EDID(dev, &edid, 256);
1324 if (ACPI_FAILURE(status)) {
1328 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1329 for (i = 0; i < edid->buffer.length; i++)
1330 seq_putc(seq, edid->buffer.pointer[i]);
1335 seq_printf(seq, "<not supported>\n");
1343 acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
1345 return single_open(file, acpi_video_device_EDID_seq_show,
1349 static int acpi_video_device_add_fs(struct acpi_device *device)
1351 struct proc_dir_entry *entry, *device_dir;
1352 struct acpi_video_device *vid_dev;
1354 vid_dev = acpi_driver_data(device);
1358 device_dir = proc_mkdir(acpi_device_bid(device),
1359 vid_dev->video->dir);
1364 entry = proc_create_data("info", S_IRUGO, device_dir,
1365 &acpi_video_device_info_fops, acpi_driver_data(device));
1367 goto err_remove_dir;
1370 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
1372 &acpi_video_device_state_fops,
1373 acpi_driver_data(device));
1375 goto err_remove_info;
1377 /* 'brightness' [R/W] */
1378 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
1380 &acpi_video_device_brightness_fops,
1381 acpi_driver_data(device));
1383 goto err_remove_state;
1386 entry = proc_create_data("EDID", S_IRUGO, device_dir,
1387 &acpi_video_device_EDID_fops,
1388 acpi_driver_data(device));
1390 goto err_remove_brightness;
1392 acpi_device_dir(device) = device_dir;
1396 err_remove_brightness:
1397 remove_proc_entry("brightness", device_dir);
1399 remove_proc_entry("state", device_dir);
1401 remove_proc_entry("info", device_dir);
1403 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1407 static int acpi_video_device_remove_fs(struct acpi_device *device)
1409 struct acpi_video_device *vid_dev;
1410 struct proc_dir_entry *device_dir;
1412 vid_dev = acpi_driver_data(device);
1413 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
1416 device_dir = acpi_device_dir(device);
1418 remove_proc_entry("info", device_dir);
1419 remove_proc_entry("state", device_dir);
1420 remove_proc_entry("brightness", device_dir);
1421 remove_proc_entry("EDID", device_dir);
1422 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1423 acpi_device_dir(device) = NULL;
1430 static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
1432 struct acpi_video_bus *video = seq->private;
1438 seq_printf(seq, "Switching heads: %s\n",
1439 video->flags.multihead ? "yes" : "no");
1440 seq_printf(seq, "Video ROM: %s\n",
1441 video->flags.rom ? "yes" : "no");
1442 seq_printf(seq, "Device to be POSTed on boot: %s\n",
1443 video->flags.post ? "yes" : "no");
1449 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
1451 return single_open(file, acpi_video_bus_info_seq_show,
1455 static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
1457 struct acpi_video_bus *video = seq->private;
1463 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
1464 seq_printf(seq, "<TODO>\n");
1470 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
1472 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1475 static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
1477 struct acpi_video_bus *video = seq->private;
1478 unsigned long long options;
1485 status = acpi_video_bus_POST_options(video, &options);
1486 if (ACPI_SUCCESS(status)) {
1487 if (!(options & 1)) {
1488 printk(KERN_WARNING PREFIX
1489 "The motherboard VGA device is not listed as a possible POST device.\n");
1490 printk(KERN_WARNING PREFIX
1491 "This indicates a BIOS bug. Please contact the manufacturer.\n");
1493 printk(KERN_WARNING "%llx\n", options);
1494 seq_printf(seq, "can POST: <integrated video>");
1496 seq_printf(seq, " <PCI video>");
1498 seq_printf(seq, " <AGP video>");
1499 seq_putc(seq, '\n');
1501 seq_printf(seq, "<not supported>\n");
1507 acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
1509 return single_open(file, acpi_video_bus_POST_info_seq_show,
1513 static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
1515 struct acpi_video_bus *video = seq->private;
1517 unsigned long long id;
1523 status = acpi_video_bus_get_POST(video, &id);
1524 if (!ACPI_SUCCESS(status)) {
1525 seq_printf(seq, "<not supported>\n");
1528 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
1534 static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
1536 struct acpi_video_bus *video = seq->private;
1539 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
1544 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
1546 return single_open(file, acpi_video_bus_POST_seq_show,
1550 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
1552 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1556 acpi_video_bus_write_POST(struct file *file,
1557 const char __user * buffer,
1558 size_t count, loff_t * data)
1561 struct seq_file *m = file->private_data;
1562 struct acpi_video_bus *video = m->private;
1563 char str[12] = { 0 };
1564 unsigned long long opt, options;
1567 if (!video || count >= sizeof(str))
1570 status = acpi_video_bus_POST_options(video, &options);
1571 if (!ACPI_SUCCESS(status))
1574 if (copy_from_user(str, buffer, count))
1578 opt = strtoul(str, NULL, 0);
1582 /* just in case an OEM 'forgot' the motherboard... */
1585 if (options & (1ul << opt)) {
1586 status = acpi_video_bus_set_POST(video, opt);
1587 if (!ACPI_SUCCESS(status))
1596 acpi_video_bus_write_DOS(struct file *file,
1597 const char __user * buffer,
1598 size_t count, loff_t * data)
1601 struct seq_file *m = file->private_data;
1602 struct acpi_video_bus *video = m->private;
1603 char str[12] = { 0 };
1607 if (!video || count >= sizeof(str))
1610 if (copy_from_user(str, buffer, count))
1614 opt = strtoul(str, NULL, 0);
1618 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
1620 if (!ACPI_SUCCESS(status))
1626 static int acpi_video_bus_add_fs(struct acpi_device *device)
1628 struct acpi_video_bus *video = acpi_driver_data(device);
1629 struct proc_dir_entry *device_dir;
1630 struct proc_dir_entry *entry;
1632 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1637 entry = proc_create_data("info", S_IRUGO, device_dir,
1638 &acpi_video_bus_info_fops,
1639 acpi_driver_data(device));
1641 goto err_remove_dir;
1644 entry = proc_create_data("ROM", S_IRUGO, device_dir,
1645 &acpi_video_bus_ROM_fops,
1646 acpi_driver_data(device));
1648 goto err_remove_info;
1650 /* 'POST_info' [R] */
1651 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
1652 &acpi_video_bus_POST_info_fops,
1653 acpi_driver_data(device));
1655 goto err_remove_rom;
1658 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
1660 &acpi_video_bus_POST_fops,
1661 acpi_driver_data(device));
1663 goto err_remove_post_info;
1666 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
1668 &acpi_video_bus_DOS_fops,
1669 acpi_driver_data(device));
1671 goto err_remove_post;
1673 video->dir = acpi_device_dir(device) = device_dir;
1677 remove_proc_entry("POST", device_dir);
1678 err_remove_post_info:
1679 remove_proc_entry("POST_info", device_dir);
1681 remove_proc_entry("ROM", device_dir);
1683 remove_proc_entry("info", device_dir);
1685 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1689 static int acpi_video_bus_remove_fs(struct acpi_device *device)
1691 struct proc_dir_entry *device_dir = acpi_device_dir(device);
1694 remove_proc_entry("info", device_dir);
1695 remove_proc_entry("ROM", device_dir);
1696 remove_proc_entry("POST_info", device_dir);
1697 remove_proc_entry("POST", device_dir);
1698 remove_proc_entry("DOS", device_dir);
1699 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1700 acpi_device_dir(device) = NULL;
1706 /* --------------------------------------------------------------------------
1708 -------------------------------------------------------------------------- */
1710 /* device interface */
1711 static struct acpi_video_device_attrib*
1712 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1714 struct acpi_video_enumerated_device *ids;
1717 for (i = 0; i < video->attached_count; i++) {
1718 ids = &video->attached_array[i];
1719 if ((ids->value.int_val & 0xffff) == device_id)
1720 return &ids->value.attrib;
1727 acpi_video_bus_get_one_device(struct acpi_device *device,
1728 struct acpi_video_bus *video)
1730 unsigned long long device_id;
1732 struct acpi_video_device *data;
1733 struct acpi_video_device_attrib* attribute;
1735 if (!device || !video)
1739 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1740 if (ACPI_SUCCESS(status)) {
1742 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1746 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1747 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1748 device->driver_data = data;
1750 data->device_id = device_id;
1751 data->video = video;
1754 attribute = acpi_video_get_device_attr(video, device_id);
1756 if((attribute != NULL) && attribute->device_id_scheme) {
1757 switch (attribute->display_type) {
1758 case ACPI_VIDEO_DISPLAY_CRT:
1759 data->flags.crt = 1;
1761 case ACPI_VIDEO_DISPLAY_TV:
1762 data->flags.tvout = 1;
1764 case ACPI_VIDEO_DISPLAY_DVI:
1765 data->flags.dvi = 1;
1767 case ACPI_VIDEO_DISPLAY_LCD:
1768 data->flags.lcd = 1;
1771 data->flags.unknown = 1;
1774 if(attribute->bios_can_detect)
1775 data->flags.bios = 1;
1777 data->flags.unknown = 1;
1779 acpi_video_device_bind(video, data);
1780 acpi_video_device_find_cap(data);
1782 status = acpi_install_notify_handler(device->handle,
1784 acpi_video_device_notify,
1786 if (ACPI_FAILURE(status)) {
1787 printk(KERN_ERR PREFIX
1788 "Error installing notify handler\n");
1789 if(data->brightness)
1790 kfree(data->brightness->levels);
1791 kfree(data->brightness);
1796 mutex_lock(&video->device_list_lock);
1797 list_add_tail(&data->entry, &video->video_device_list);
1798 mutex_unlock(&video->device_list_lock);
1800 acpi_video_device_add_fs(device);
1810 * video : video bus device
1815 * Enumerate the video device list of the video bus,
1816 * bind the ids with the corresponding video devices
1817 * under the video bus.
1820 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1822 struct acpi_video_device *dev;
1824 mutex_lock(&video->device_list_lock);
1826 list_for_each_entry(dev, &video->video_device_list, entry)
1827 acpi_video_device_bind(video, dev);
1829 mutex_unlock(&video->device_list_lock);
1834 * video : video bus device
1835 * device : video output device under the video
1841 * Bind the ids with the corresponding video devices
1842 * under the video bus.
1846 acpi_video_device_bind(struct acpi_video_bus *video,
1847 struct acpi_video_device *device)
1849 struct acpi_video_enumerated_device *ids;
1852 for (i = 0; i < video->attached_count; i++) {
1853 ids = &video->attached_array[i];
1854 if (device->device_id == (ids->value.int_val & 0xffff)) {
1855 ids->bind_info = device;
1856 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1863 * video : video bus device
1868 * Call _DOD to enumerate all devices attached to display adapter
1872 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1877 struct acpi_video_enumerated_device *active_list;
1878 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1879 union acpi_object *dod = NULL;
1880 union acpi_object *obj;
1882 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1883 if (!ACPI_SUCCESS(status)) {
1884 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1888 dod = buffer.pointer;
1889 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1890 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1895 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1896 dod->package.count));
1898 active_list = kcalloc(1 + dod->package.count,
1899 sizeof(struct acpi_video_enumerated_device),
1907 for (i = 0; i < dod->package.count; i++) {
1908 obj = &dod->package.elements[i];
1910 if (obj->type != ACPI_TYPE_INTEGER) {
1911 printk(KERN_ERR PREFIX
1912 "Invalid _DOD data in element %d\n", i);
1916 active_list[count].value.int_val = obj->integer.value;
1917 active_list[count].bind_info = NULL;
1918 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1919 (int)obj->integer.value));
1923 kfree(video->attached_array);
1925 video->attached_array = active_list;
1926 video->attached_count = count;
1929 kfree(buffer.pointer);
1934 acpi_video_get_next_level(struct acpi_video_device *device,
1935 u32 level_current, u32 event)
1937 int min, max, min_above, max_below, i, l, delta = 255;
1938 max = max_below = 0;
1939 min = min_above = 255;
1940 /* Find closest level to level_current */
1941 for (i = 2; i < device->brightness->count; i++) {
1942 l = device->brightness->levels[i];
1943 if (abs(l - level_current) < abs(delta)) {
1944 delta = l - level_current;
1949 /* Ajust level_current to closest available level */
1950 level_current += delta;
1951 for (i = 2; i < device->brightness->count; i++) {
1952 l = device->brightness->levels[i];
1957 if (l < min_above && l > level_current)
1959 if (l > max_below && l < level_current)
1964 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1965 return (level_current < max) ? min_above : min;
1966 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1967 return (level_current < max) ? min_above : max;
1968 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1969 return (level_current > min) ? max_below : min;
1970 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1971 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1974 return level_current;
1979 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1981 unsigned long long level_current, level_next;
1982 int result = -EINVAL;
1984 /* no warning message if acpi_backlight=vendor is used */
1985 if (!acpi_video_backlight_support())
1988 if (!device->brightness)
1991 result = acpi_video_device_lcd_get_level_current(device,
1996 level_next = acpi_video_get_next_level(device, level_current, event);
1998 result = acpi_video_device_lcd_set_level(device, level_next);
2001 backlight_force_update(device->backlight,
2002 BACKLIGHT_UPDATE_HOTKEY);
2006 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
2012 acpi_video_bus_get_devices(struct acpi_video_bus *video,
2013 struct acpi_device *device)
2016 struct acpi_device *dev;
2018 acpi_video_device_enumerate(video);
2020 list_for_each_entry(dev, &device->children, node) {
2022 status = acpi_video_bus_get_one_device(dev, video);
2023 if (ACPI_FAILURE(status)) {
2024 printk(KERN_WARNING PREFIX
2025 "Cant attach device");
2032 static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
2035 struct acpi_video_bus *video;
2038 if (!device || !device->video)
2041 video = device->video;
2043 acpi_video_device_remove_fs(device->dev);
2045 status = acpi_remove_notify_handler(device->dev->handle,
2047 acpi_video_device_notify);
2048 if (device->backlight) {
2049 sysfs_remove_link(&device->backlight->dev.kobj, "device");
2050 backlight_device_unregister(device->backlight);
2051 device->backlight = NULL;
2053 if (device->cooling_dev) {
2054 sysfs_remove_link(&device->dev->dev.kobj,
2056 sysfs_remove_link(&device->cooling_dev->device.kobj,
2058 thermal_cooling_device_unregister(device->cooling_dev);
2059 device->cooling_dev = NULL;
2061 video_output_unregister(device->output_dev);
2066 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
2069 struct acpi_video_device *dev, *next;
2071 mutex_lock(&video->device_list_lock);
2073 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
2075 status = acpi_video_bus_put_one_device(dev);
2076 if (ACPI_FAILURE(status))
2077 printk(KERN_WARNING PREFIX
2078 "hhuuhhuu bug in acpi video driver.\n");
2080 if (dev->brightness) {
2081 kfree(dev->brightness->levels);
2082 kfree(dev->brightness);
2084 list_del(&dev->entry);
2088 mutex_unlock(&video->device_list_lock);
2093 /* acpi_video interface */
2095 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
2097 return acpi_video_bus_DOS(video, 0, 0);
2100 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
2102 return acpi_video_bus_DOS(video, 0, 1);
2105 static void acpi_video_bus_notify(struct acpi_device *device, u32 event)
2107 struct acpi_video_bus *video = acpi_driver_data(device);
2108 struct input_dev *input;
2114 input = video->input;
2117 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
2118 * most likely via hotkey. */
2119 acpi_bus_generate_proc_event(device, event, 0);
2120 keycode = KEY_SWITCHVIDEOMODE;
2123 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
2125 acpi_video_device_enumerate(video);
2126 acpi_video_device_rebind(video);
2127 acpi_bus_generate_proc_event(device, event, 0);
2128 keycode = KEY_SWITCHVIDEOMODE;
2131 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
2132 acpi_bus_generate_proc_event(device, event, 0);
2133 keycode = KEY_SWITCHVIDEOMODE;
2135 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
2136 acpi_bus_generate_proc_event(device, event, 0);
2137 keycode = KEY_VIDEO_NEXT;
2139 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
2140 acpi_bus_generate_proc_event(device, event, 0);
2141 keycode = KEY_VIDEO_PREV;
2145 keycode = KEY_UNKNOWN;
2146 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2147 "Unsupported event [0x%x]\n", event));
2151 acpi_notifier_call_chain(device, event, 0);
2152 input_report_key(input, keycode, 1);
2154 input_report_key(input, keycode, 0);
2160 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
2162 struct acpi_video_device *video_device = data;
2163 struct acpi_device *device = NULL;
2164 struct acpi_video_bus *bus;
2165 struct input_dev *input;
2171 device = video_device->dev;
2172 bus = video_device->video;
2176 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
2177 if (brightness_switch_enabled)
2178 acpi_video_switch_brightness(video_device, event);
2179 acpi_bus_generate_proc_event(device, event, 0);
2180 keycode = KEY_BRIGHTNESS_CYCLE;
2182 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
2183 if (brightness_switch_enabled)
2184 acpi_video_switch_brightness(video_device, event);
2185 acpi_bus_generate_proc_event(device, event, 0);
2186 keycode = KEY_BRIGHTNESSUP;
2188 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
2189 if (brightness_switch_enabled)
2190 acpi_video_switch_brightness(video_device, event);
2191 acpi_bus_generate_proc_event(device, event, 0);
2192 keycode = KEY_BRIGHTNESSDOWN;
2194 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
2195 if (brightness_switch_enabled)
2196 acpi_video_switch_brightness(video_device, event);
2197 acpi_bus_generate_proc_event(device, event, 0);
2198 keycode = KEY_BRIGHTNESS_ZERO;
2200 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
2201 if (brightness_switch_enabled)
2202 acpi_video_switch_brightness(video_device, event);
2203 acpi_bus_generate_proc_event(device, event, 0);
2204 keycode = KEY_DISPLAY_OFF;
2207 keycode = KEY_UNKNOWN;
2208 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2209 "Unsupported event [0x%x]\n", event));
2213 acpi_notifier_call_chain(device, event, 0);
2214 input_report_key(input, keycode, 1);
2216 input_report_key(input, keycode, 0);
2222 static int instance;
2223 static int acpi_video_resume(struct acpi_device *device)
2225 struct acpi_video_bus *video;
2226 struct acpi_video_device *video_device;
2229 if (!device || !acpi_driver_data(device))
2232 video = acpi_driver_data(device);
2234 for (i = 0; i < video->attached_count; i++) {
2235 video_device = video->attached_array[i].bind_info;
2236 if (video_device && video_device->backlight)
2237 acpi_video_set_brightness(video_device->backlight);
2242 static int acpi_video_bus_add(struct acpi_device *device)
2244 struct acpi_video_bus *video;
2245 struct input_dev *input;
2248 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
2252 /* a hack to fix the duplicate name "VID" problem on T61 */
2253 if (!strcmp(device->pnp.bus_id, "VID")) {
2255 device->pnp.bus_id[3] = '0' + instance;
2258 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2259 if (!strcmp(device->pnp.bus_id, "VGA")) {
2261 device->pnp.bus_id[3] = '0' + instance;
2265 video->device = device;
2266 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2267 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
2268 device->driver_data = video;
2270 acpi_video_bus_find_cap(video);
2271 error = acpi_video_bus_check(video);
2273 goto err_free_video;
2275 error = acpi_video_bus_add_fs(device);
2277 goto err_free_video;
2279 mutex_init(&video->device_list_lock);
2280 INIT_LIST_HEAD(&video->video_device_list);
2282 acpi_video_bus_get_devices(video, device);
2283 acpi_video_bus_start_devices(video);
2285 video->input = input = input_allocate_device();
2288 goto err_stop_video;
2291 snprintf(video->phys, sizeof(video->phys),
2292 "%s/video/input0", acpi_device_hid(video->device));
2294 input->name = acpi_device_name(video->device);
2295 input->phys = video->phys;
2296 input->id.bustype = BUS_HOST;
2297 input->id.product = 0x06;
2298 input->dev.parent = &device->dev;
2299 input->evbit[0] = BIT(EV_KEY);
2300 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2301 set_bit(KEY_VIDEO_NEXT, input->keybit);
2302 set_bit(KEY_VIDEO_PREV, input->keybit);
2303 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2304 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2305 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2306 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2307 set_bit(KEY_DISPLAY_OFF, input->keybit);
2308 set_bit(KEY_UNKNOWN, input->keybit);
2310 error = input_register_device(input);
2312 goto err_free_input_dev;
2314 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
2315 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2316 video->flags.multihead ? "yes" : "no",
2317 video->flags.rom ? "yes" : "no",
2318 video->flags.post ? "yes" : "no");
2323 input_free_device(input);
2325 acpi_video_bus_stop_devices(video);
2326 acpi_video_bus_put_devices(video);
2327 kfree(video->attached_array);
2328 acpi_video_bus_remove_fs(device);
2331 device->driver_data = NULL;
2336 static int acpi_video_bus_remove(struct acpi_device *device, int type)
2338 struct acpi_video_bus *video = NULL;
2341 if (!device || !acpi_driver_data(device))
2344 video = acpi_driver_data(device);
2346 acpi_video_bus_stop_devices(video);
2347 acpi_video_bus_put_devices(video);
2348 acpi_video_bus_remove_fs(device);
2350 input_unregister_device(video->input);
2351 kfree(video->attached_array);
2357 static int __init intel_opregion_present(void)
2359 #if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2360 struct pci_dev *dev = NULL;
2363 for_each_pci_dev(dev) {
2364 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2366 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2368 pci_read_config_dword(dev, 0xfc, &address);
2377 int acpi_video_register(void)
2380 if (register_count) {
2382 * if the function of acpi_video_register is already called,
2383 * don't register the acpi_vide_bus again and return no error.
2388 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2389 if (!acpi_video_dir)
2392 result = acpi_bus_register_driver(&acpi_video_bus);
2394 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2399 * When the acpi_video_bus is loaded successfully, increase
2400 * the counter reference.
2406 EXPORT_SYMBOL(acpi_video_register);
2408 void acpi_video_unregister(void)
2410 if (!register_count) {
2412 * If the acpi video bus is already unloaded, don't
2413 * unload it again and return directly.
2417 acpi_bus_unregister_driver(&acpi_video_bus);
2419 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2425 EXPORT_SYMBOL(acpi_video_unregister);
2428 * This is kind of nasty. Hardware using Intel chipsets may require
2429 * the video opregion code to be run first in order to initialise
2430 * state before any ACPI video calls are made. To handle this we defer
2431 * registration of the video class until the opregion code has run.
2434 static int __init acpi_video_init(void)
2436 dmi_check_system(video_dmi_table);
2438 if (intel_opregion_present())
2441 return acpi_video_register();
2444 static void __exit acpi_video_exit(void)
2446 acpi_video_unregister();
2451 module_init(acpi_video_init);
2452 module_exit(acpi_video_exit);