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);
83 static const struct acpi_device_id video_device_ids[] = {
87 MODULE_DEVICE_TABLE(acpi, video_device_ids);
89 static struct acpi_driver acpi_video_bus = {
91 .class = ACPI_VIDEO_CLASS,
92 .ids = video_device_ids,
94 .add = acpi_video_bus_add,
95 .remove = acpi_video_bus_remove,
96 .resume = acpi_video_resume,
100 struct acpi_video_bus_flags {
101 u8 multihead:1; /* can switch video heads */
102 u8 rom:1; /* can retrieve a video rom */
103 u8 post:1; /* can configure the head to */
107 struct acpi_video_bus_cap {
108 u8 _DOS:1; /*Enable/Disable output switching */
109 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
110 u8 _ROM:1; /*Get ROM Data */
111 u8 _GPD:1; /*Get POST Device */
112 u8 _SPD:1; /*Set POST Device */
113 u8 _VPO:1; /*Video POST Options */
117 struct acpi_video_device_attrib {
118 u32 display_index:4; /* A zero-based instance of the Display */
119 u32 display_port_attachment:4; /*This field differentiates the display type */
120 u32 display_type:4; /*Describe the specific type in use */
121 u32 vendor_specific:4; /*Chipset Vendor Specific */
122 u32 bios_can_detect:1; /*BIOS can detect the device */
123 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
125 u32 pipe_id:3; /*For VGA multiple-head devices. */
126 u32 reserved:10; /*Must be 0 */
127 u32 device_id_scheme:1; /*Device ID Scheme */
130 struct acpi_video_enumerated_device {
133 struct acpi_video_device_attrib attrib;
135 struct acpi_video_device *bind_info;
138 struct acpi_video_bus {
139 struct acpi_device *device;
141 struct acpi_video_enumerated_device *attached_array;
143 struct acpi_video_bus_cap cap;
144 struct acpi_video_bus_flags flags;
145 struct list_head video_device_list;
146 struct mutex device_list_lock; /* protects video_device_list */
147 struct proc_dir_entry *dir;
148 struct input_dev *input;
149 char phys[32]; /* for input device */
152 struct acpi_video_device_flags {
162 struct acpi_video_device_cap {
163 u8 _ADR:1; /*Return the unique ID */
164 u8 _BCL:1; /*Query list of brightness control levels supported */
165 u8 _BCM:1; /*Set the brightness level */
166 u8 _BQC:1; /* Get current brightness level */
167 u8 _BCQ:1; /* Some buggy BIOS uses _BCQ instead of _BQC */
168 u8 _DDC:1; /*Return the EDID for this device */
169 u8 _DCS:1; /*Return status of output device */
170 u8 _DGS:1; /*Query graphics state */
171 u8 _DSS:1; /*Device state set */
174 struct acpi_video_brightness_flags {
175 u8 _BCL_no_ac_battery_levels:1; /* no AC/Battery levels in _BCL */
176 u8 _BCL_reversed:1; /* _BCL package is in a reversed order*/
177 u8 _BCL_use_index:1; /* levels in _BCL are index values */
178 u8 _BCM_use_index:1; /* input of _BCM is an index value */
179 u8 _BQC_use_index:1; /* _BQC returns an index value */
182 struct acpi_video_device_brightness {
186 struct acpi_video_brightness_flags flags;
189 struct acpi_video_device {
190 unsigned long device_id;
191 struct acpi_video_device_flags flags;
192 struct acpi_video_device_cap cap;
193 struct list_head entry;
194 struct acpi_video_bus *video;
195 struct acpi_device *dev;
196 struct acpi_video_device_brightness *brightness;
197 struct backlight_device *backlight;
198 struct thermal_cooling_device *cdev;
199 struct output_device *output_dev;
203 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
204 static const struct file_operations acpi_video_bus_info_fops = {
205 .owner = THIS_MODULE,
206 .open = acpi_video_bus_info_open_fs,
209 .release = single_release,
212 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
213 static const struct file_operations acpi_video_bus_ROM_fops = {
214 .owner = THIS_MODULE,
215 .open = acpi_video_bus_ROM_open_fs,
218 .release = single_release,
221 static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
223 static const struct file_operations acpi_video_bus_POST_info_fops = {
224 .owner = THIS_MODULE,
225 .open = acpi_video_bus_POST_info_open_fs,
228 .release = single_release,
231 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
232 static ssize_t acpi_video_bus_write_POST(struct file *file,
233 const char __user *buffer, size_t count, loff_t *data);
234 static const struct file_operations acpi_video_bus_POST_fops = {
235 .owner = THIS_MODULE,
236 .open = acpi_video_bus_POST_open_fs,
238 .write = acpi_video_bus_write_POST,
240 .release = single_release,
243 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
244 static ssize_t acpi_video_bus_write_DOS(struct file *file,
245 const char __user *buffer, size_t count, loff_t *data);
246 static const struct file_operations acpi_video_bus_DOS_fops = {
247 .owner = THIS_MODULE,
248 .open = acpi_video_bus_DOS_open_fs,
250 .write = acpi_video_bus_write_DOS,
252 .release = single_release,
256 static int acpi_video_device_info_open_fs(struct inode *inode,
258 static const struct file_operations acpi_video_device_info_fops = {
259 .owner = THIS_MODULE,
260 .open = acpi_video_device_info_open_fs,
263 .release = single_release,
266 static int acpi_video_device_state_open_fs(struct inode *inode,
268 static ssize_t acpi_video_device_write_state(struct file *file,
269 const char __user *buffer, size_t count, loff_t *data);
270 static const struct file_operations acpi_video_device_state_fops = {
271 .owner = THIS_MODULE,
272 .open = acpi_video_device_state_open_fs,
274 .write = acpi_video_device_write_state,
276 .release = single_release,
279 static int acpi_video_device_brightness_open_fs(struct inode *inode,
281 static ssize_t acpi_video_device_write_brightness(struct file *file,
282 const char __user *buffer, size_t count, loff_t *data);
283 static struct file_operations acpi_video_device_brightness_fops = {
284 .owner = THIS_MODULE,
285 .open = acpi_video_device_brightness_open_fs,
287 .write = acpi_video_device_write_brightness,
289 .release = single_release,
292 static int acpi_video_device_EDID_open_fs(struct inode *inode,
294 static const struct file_operations acpi_video_device_EDID_fops = {
295 .owner = THIS_MODULE,
296 .open = acpi_video_device_EDID_open_fs,
299 .release = single_release,
302 static const char device_decode[][30] = {
303 "motherboard VGA device",
309 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
310 static void acpi_video_device_rebind(struct acpi_video_bus *video);
311 static void acpi_video_device_bind(struct acpi_video_bus *video,
312 struct acpi_video_device *device);
313 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
314 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
316 static int acpi_video_device_lcd_get_level_current(
317 struct acpi_video_device *device,
318 unsigned long long *level);
319 static int acpi_video_get_next_level(struct acpi_video_device *device,
320 u32 level_current, u32 event);
321 static int acpi_video_switch_brightness(struct acpi_video_device *device,
323 static int acpi_video_device_get_state(struct acpi_video_device *device,
324 unsigned long long *state);
325 static int acpi_video_output_get(struct output_device *od);
326 static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
328 /*backlight device sysfs support*/
329 static int acpi_video_get_brightness(struct backlight_device *bd)
331 unsigned long long cur_level;
333 struct acpi_video_device *vd =
334 (struct acpi_video_device *)bl_get_data(bd);
336 if (acpi_video_device_lcd_get_level_current(vd, &cur_level))
338 for (i = 2; i < vd->brightness->count; i++) {
339 if (vd->brightness->levels[i] == cur_level)
340 /* The first two entries are special - see page 575
341 of the ACPI spec 3.0 */
347 static int acpi_video_set_brightness(struct backlight_device *bd)
349 int request_level = bd->props.brightness + 2;
350 struct acpi_video_device *vd =
351 (struct acpi_video_device *)bl_get_data(bd);
353 return acpi_video_device_lcd_set_level(vd,
354 vd->brightness->levels[request_level]);
357 static struct backlight_ops acpi_backlight_ops = {
358 .get_brightness = acpi_video_get_brightness,
359 .update_status = acpi_video_set_brightness,
362 /*video output device sysfs support*/
363 static int acpi_video_output_get(struct output_device *od)
365 unsigned long long state;
366 struct acpi_video_device *vd =
367 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
368 acpi_video_device_get_state(vd, &state);
372 static int acpi_video_output_set(struct output_device *od)
374 unsigned long state = od->request_state;
375 struct acpi_video_device *vd=
376 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
377 return acpi_video_device_set_state(vd, state);
380 static struct output_properties acpi_output_properties = {
381 .set_state = acpi_video_output_set,
382 .get_status = acpi_video_output_get,
386 /* thermal cooling device callbacks */
387 static int video_get_max_state(struct thermal_cooling_device *cdev, unsigned
390 struct acpi_device *device = cdev->devdata;
391 struct acpi_video_device *video = acpi_driver_data(device);
393 *state = video->brightness->count - 3;
397 static int video_get_cur_state(struct thermal_cooling_device *cdev, unsigned
400 struct acpi_device *device = cdev->devdata;
401 struct acpi_video_device *video = acpi_driver_data(device);
402 unsigned long long level;
405 if (acpi_video_device_lcd_get_level_current(video, &level))
407 for (offset = 2; offset < video->brightness->count; offset++)
408 if (level == video->brightness->levels[offset]) {
409 *state = video->brightness->count - offset - 1;
417 video_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
419 struct acpi_device *device = cdev->devdata;
420 struct acpi_video_device *video = acpi_driver_data(device);
423 if ( state >= video->brightness->count - 2)
426 state = video->brightness->count - state;
427 level = video->brightness->levels[state -1];
428 return acpi_video_device_lcd_set_level(video, level);
431 static struct thermal_cooling_device_ops video_cooling_ops = {
432 .get_max_state = video_get_max_state,
433 .get_cur_state = video_get_cur_state,
434 .set_cur_state = video_set_cur_state,
437 /* --------------------------------------------------------------------------
439 -------------------------------------------------------------------------- */
444 acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
448 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
454 acpi_video_device_get_state(struct acpi_video_device *device,
455 unsigned long long *state)
459 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
465 acpi_video_device_set_state(struct acpi_video_device *device, int state)
468 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
469 struct acpi_object_list args = { 1, &arg0 };
470 unsigned long long ret;
473 arg0.integer.value = state;
474 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
480 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
481 union acpi_object **levels)
484 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
485 union acpi_object *obj;
490 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
491 if (!ACPI_SUCCESS(status))
493 obj = (union acpi_object *)buffer.pointer;
494 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
495 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
505 kfree(buffer.pointer);
511 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
514 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
515 struct acpi_object_list args = { 1, &arg0 };
518 arg0.integer.value = level;
520 status = acpi_evaluate_object(device->dev->handle, "_BCM",
522 if (ACPI_FAILURE(status)) {
523 ACPI_ERROR((AE_INFO, "Evaluating _BCM failed"));
527 device->brightness->curr = level;
528 for (state = 2; state < device->brightness->count; state++)
529 if (level == device->brightness->levels[state]) {
530 if (device->backlight)
531 device->backlight->props.brightness = state - 2;
535 ACPI_ERROR((AE_INFO, "Current brightness invalid"));
540 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
541 unsigned long long *level)
543 acpi_status status = AE_OK;
545 if (device->cap._BQC || device->cap._BCQ) {
546 char *buf = device->cap._BQC ? "_BQC" : "_BCQ";
548 status = acpi_evaluate_integer(device->dev->handle, buf,
550 if (ACPI_SUCCESS(status)) {
551 if (device->brightness->flags._BQC_use_index) {
552 if (device->brightness->flags._BCL_reversed)
553 *level = device->brightness->count
555 *level = device->brightness->levels[*level + 2];
558 device->brightness->curr = *level;
562 * should we return an error or ignore this failure?
563 * dev->brightness->curr is a cached value which stores
564 * the correct current backlight level in most cases.
565 * ACPI video backlight still works w/ buggy _BQC.
566 * http://bugzilla.kernel.org/show_bug.cgi?id=12233
568 ACPI_WARNING((AE_INFO, "Evaluating %s failed", buf));
569 device->cap._BQC = device->cap._BCQ = 0;
573 *level = device->brightness->curr;
578 acpi_video_device_EDID(struct acpi_video_device *device,
579 union acpi_object **edid, ssize_t length)
582 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
583 union acpi_object *obj;
584 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
585 struct acpi_object_list args = { 1, &arg0 };
593 arg0.integer.value = 1;
594 else if (length == 256)
595 arg0.integer.value = 2;
599 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
600 if (ACPI_FAILURE(status))
603 obj = buffer.pointer;
605 if (obj && obj->type == ACPI_TYPE_BUFFER)
608 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
619 acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
622 unsigned long long tmp;
623 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
624 struct acpi_object_list args = { 1, &arg0 };
627 arg0.integer.value = option;
629 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
630 if (ACPI_SUCCESS(status))
631 status = tmp ? (-EINVAL) : (AE_OK);
637 acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
641 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
647 acpi_video_bus_POST_options(struct acpi_video_bus *video,
648 unsigned long long *options)
652 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
660 * video : video bus device pointer
662 * 0. The system BIOS should NOT automatically switch(toggle)
663 * the active display output.
664 * 1. The system BIOS should automatically switch (toggle) the
665 * active display output. No switch event.
666 * 2. The _DGS value should be locked.
667 * 3. The system BIOS should not automatically switch (toggle) the
668 * active display output, but instead generate the display switch
671 * 0. The system BIOS should automatically control the brightness level
672 * of the LCD when the power changes from AC to DC
673 * 1. The system BIOS should NOT automatically control the brightness
674 * level of the LCD when the power changes from AC to DC.
680 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
682 acpi_integer status = 0;
683 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
684 struct acpi_object_list args = { 1, &arg0 };
687 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
691 arg0.integer.value = (lcd_flag << 2) | bios_flag;
692 video->dos_setting = arg0.integer.value;
693 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
700 * Simple comparison function used to sort backlight levels.
704 acpi_video_cmp_level(const void *a, const void *b)
706 return *(int *)a - *(int *)b;
711 * device : video output device (LCD, CRT, ..)
714 * Maximum brightness level
716 * Allocate and initialize device->brightness.
720 acpi_video_init_brightness(struct acpi_video_device *device)
722 union acpi_object *obj = NULL;
723 int i, max_level = 0, count = 0, level_ac_battery = 0;
724 unsigned long long level, level_old;
725 union acpi_object *o;
726 struct acpi_video_device_brightness *br = NULL;
727 int result = -EINVAL;
729 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
730 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
731 "LCD brightness level\n"));
735 if (obj->package.count < 2)
738 br = kzalloc(sizeof(*br), GFP_KERNEL);
740 printk(KERN_ERR "can't allocate memory\n");
745 br->levels = kmalloc((obj->package.count + 2) * sizeof *(br->levels),
752 for (i = 0; i < obj->package.count; i++) {
753 o = (union acpi_object *)&obj->package.elements[i];
754 if (o->type != ACPI_TYPE_INTEGER) {
755 printk(KERN_ERR PREFIX "Invalid data\n");
758 br->levels[count] = (u32) o->integer.value;
760 if (br->levels[count] > max_level)
761 max_level = br->levels[count];
766 * some buggy BIOS don't export the levels
767 * when machine is on AC/Battery in _BCL package.
768 * In this case, the first two elements in _BCL packages
769 * are also supported brightness levels that OS should take care of.
771 for (i = 2; i < count; i++)
772 if (br->levels[i] == br->levels[0] ||
773 br->levels[i] == br->levels[1])
776 if (level_ac_battery < 2) {
777 level_ac_battery = 2 - level_ac_battery;
778 br->flags._BCL_no_ac_battery_levels = 1;
779 for (i = (count - 1 + level_ac_battery); i >= 2; i--)
780 br->levels[i] = br->levels[i - level_ac_battery];
781 count += level_ac_battery;
782 } else if (level_ac_battery > 2)
783 ACPI_ERROR((AE_INFO, "Too many duplicates in _BCL package\n"));
785 /* Check if the _BCL package is in a reversed order */
786 if (max_level == br->levels[2]) {
787 br->flags._BCL_reversed = 1;
788 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
789 acpi_video_cmp_level, NULL);
790 } else if (max_level != br->levels[count - 1])
792 "Found unordered _BCL package\n"));
795 device->brightness = br;
797 /* Check the input/output of _BQC/_BCL/_BCM */
798 if ((max_level < 100) && (max_level <= (count - 2)))
799 br->flags._BCL_use_index = 1;
802 * _BCM is always consistent with _BCL,
803 * at least for all the laptops we have ever seen.
805 br->flags._BCM_use_index = br->flags._BCL_use_index;
807 /* _BQC uses INDEX while _BCL uses VALUE in some laptops */
808 br->curr = max_level;
809 result = acpi_video_device_lcd_get_level_current(device, &level_old);
811 goto out_free_levels;
813 result = acpi_video_device_lcd_set_level(device, br->curr);
815 goto out_free_levels;
817 result = acpi_video_device_lcd_get_level_current(device, &level);
819 goto out_free_levels;
821 if ((level != level_old) && !br->flags._BCM_use_index) {
823 * This piece of code does not work correctly if the current
824 * brightness levels is 0.
825 * But I guess boxes that boot with such a dark screen are rare
826 * and no more code is needed to cover this specifial case.
829 if (level_ac_battery != 2) {
831 * For now, we don't support the _BCL like this:
832 * 16, 15, 0, 1, 2, 3, ..., 14, 15, 16
833 * because we may mess up the index returned by _BQC.
834 * Plus: we have not got a box like this.
836 ACPI_ERROR((AE_INFO, "_BCL not supported\n"));
838 br->flags._BQC_use_index = 1;
841 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
842 "found %d brightness levels\n", count - 2));
851 device->brightness = NULL;
858 * device : video output device (LCD, CRT, ..)
863 * Find out all required AML methods defined under the output
867 static void acpi_video_device_find_cap(struct acpi_video_device *device)
869 acpi_handle h_dummy1;
872 memset(&device->cap, 0, sizeof(device->cap));
874 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
875 device->cap._ADR = 1;
877 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
878 device->cap._BCL = 1;
880 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
881 device->cap._BCM = 1;
883 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
884 device->cap._BQC = 1;
885 else if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCQ",
887 printk(KERN_WARNING FW_BUG "_BCQ is used instead of _BQC\n");
888 device->cap._BCQ = 1;
891 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
892 device->cap._DDC = 1;
894 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
895 device->cap._DCS = 1;
897 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
898 device->cap._DGS = 1;
900 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
901 device->cap._DSS = 1;
904 if (acpi_video_backlight_support()) {
906 static int count = 0;
909 result = acpi_video_init_brightness(device);
912 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
916 sprintf(name, "acpi_video%d", count++);
917 device->backlight = backlight_device_register(name,
918 NULL, device, &acpi_backlight_ops);
919 device->backlight->props.max_brightness = device->brightness->count-3;
922 device->cdev = thermal_cooling_device_register("LCD",
923 device->dev, &video_cooling_ops);
924 if (IS_ERR(device->cdev))
927 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
929 result = sysfs_create_link(&device->dev->dev.kobj,
930 &device->cdev->device.kobj,
933 printk(KERN_ERR PREFIX "Create sysfs link\n");
934 result = sysfs_create_link(&device->cdev->device.kobj,
935 &device->dev->dev.kobj, "device");
937 printk(KERN_ERR PREFIX "Create sysfs link\n");
941 if (acpi_video_display_switch_support()) {
943 if (device->cap._DCS && device->cap._DSS) {
946 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
949 sprintf(name, "acpi_video%d", count++);
950 device->output_dev = video_output_register(name,
951 NULL, device, &acpi_output_properties);
959 * device : video output device (VGA)
964 * Find out all required AML methods defined under the video bus device.
967 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
969 acpi_handle h_dummy1;
971 memset(&video->cap, 0, sizeof(video->cap));
972 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
975 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
978 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
981 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
984 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
987 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
993 * Check whether the video bus device has required AML method to
994 * support the desired features
997 static int acpi_video_bus_check(struct acpi_video_bus *video)
999 acpi_status status = -ENOENT;
1005 dev = acpi_get_physical_pci_device(video->device->handle);
1010 /* Since there is no HID, CID and so on for VGA driver, we have
1011 * to check well known required nodes.
1014 /* Does this device support video switching? */
1015 if (video->cap._DOS) {
1016 video->flags.multihead = 1;
1020 /* Does this device support retrieving a video ROM? */
1021 if (video->cap._ROM) {
1022 video->flags.rom = 1;
1026 /* Does this device support configuring which video device to POST? */
1027 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
1028 video->flags.post = 1;
1035 /* --------------------------------------------------------------------------
1036 FS Interface (/proc)
1037 -------------------------------------------------------------------------- */
1039 static struct proc_dir_entry *acpi_video_dir;
1043 static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
1045 struct acpi_video_device *dev = seq->private;
1051 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
1052 seq_printf(seq, "type: ");
1054 seq_printf(seq, "CRT\n");
1055 else if (dev->flags.lcd)
1056 seq_printf(seq, "LCD\n");
1057 else if (dev->flags.tvout)
1058 seq_printf(seq, "TVOUT\n");
1059 else if (dev->flags.dvi)
1060 seq_printf(seq, "DVI\n");
1062 seq_printf(seq, "UNKNOWN\n");
1064 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
1071 acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
1073 return single_open(file, acpi_video_device_info_seq_show,
1077 static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
1080 struct acpi_video_device *dev = seq->private;
1081 unsigned long long state;
1087 status = acpi_video_device_get_state(dev, &state);
1088 seq_printf(seq, "state: ");
1089 if (ACPI_SUCCESS(status))
1090 seq_printf(seq, "0x%02llx\n", state);
1092 seq_printf(seq, "<not supported>\n");
1094 status = acpi_video_device_query(dev, &state);
1095 seq_printf(seq, "query: ");
1096 if (ACPI_SUCCESS(status))
1097 seq_printf(seq, "0x%02llx\n", state);
1099 seq_printf(seq, "<not supported>\n");
1106 acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
1108 return single_open(file, acpi_video_device_state_seq_show,
1113 acpi_video_device_write_state(struct file *file,
1114 const char __user * buffer,
1115 size_t count, loff_t * data)
1118 struct seq_file *m = file->private_data;
1119 struct acpi_video_device *dev = m->private;
1120 char str[12] = { 0 };
1124 if (!dev || count + 1 > sizeof str)
1127 if (copy_from_user(str, buffer, count))
1131 state = simple_strtoul(str, NULL, 0);
1132 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
1134 status = acpi_video_device_set_state(dev, state);
1143 acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
1145 struct acpi_video_device *dev = seq->private;
1149 if (!dev || !dev->brightness) {
1150 seq_printf(seq, "<not supported>\n");
1154 seq_printf(seq, "levels: ");
1155 for (i = 2; i < dev->brightness->count; i++)
1156 seq_printf(seq, " %d", dev->brightness->levels[i]);
1157 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1163 acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
1165 return single_open(file, acpi_video_device_brightness_seq_show,
1170 acpi_video_device_write_brightness(struct file *file,
1171 const char __user * buffer,
1172 size_t count, loff_t * data)
1174 struct seq_file *m = file->private_data;
1175 struct acpi_video_device *dev = m->private;
1176 char str[5] = { 0 };
1177 unsigned int level = 0;
1181 if (!dev || !dev->brightness || count + 1 > sizeof str)
1184 if (copy_from_user(str, buffer, count))
1188 level = simple_strtoul(str, NULL, 0);
1193 /* validate through the list of available levels */
1194 for (i = 2; i < dev->brightness->count; i++)
1195 if (level == dev->brightness->levels[i]) {
1196 if (!acpi_video_device_lcd_set_level(dev, level))
1204 static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
1206 struct acpi_video_device *dev = seq->private;
1209 union acpi_object *edid = NULL;
1215 status = acpi_video_device_EDID(dev, &edid, 128);
1216 if (ACPI_FAILURE(status)) {
1217 status = acpi_video_device_EDID(dev, &edid, 256);
1220 if (ACPI_FAILURE(status)) {
1224 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1225 for (i = 0; i < edid->buffer.length; i++)
1226 seq_putc(seq, edid->buffer.pointer[i]);
1231 seq_printf(seq, "<not supported>\n");
1239 acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
1241 return single_open(file, acpi_video_device_EDID_seq_show,
1245 static int acpi_video_device_add_fs(struct acpi_device *device)
1247 struct proc_dir_entry *entry, *device_dir;
1248 struct acpi_video_device *vid_dev;
1250 vid_dev = acpi_driver_data(device);
1254 device_dir = proc_mkdir(acpi_device_bid(device),
1255 vid_dev->video->dir);
1259 device_dir->owner = THIS_MODULE;
1262 entry = proc_create_data("info", S_IRUGO, device_dir,
1263 &acpi_video_device_info_fops, acpi_driver_data(device));
1265 goto err_remove_dir;
1268 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
1270 &acpi_video_device_state_fops,
1271 acpi_driver_data(device));
1273 goto err_remove_info;
1275 /* 'brightness' [R/W] */
1276 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
1278 &acpi_video_device_brightness_fops,
1279 acpi_driver_data(device));
1281 goto err_remove_state;
1284 entry = proc_create_data("EDID", S_IRUGO, device_dir,
1285 &acpi_video_device_EDID_fops,
1286 acpi_driver_data(device));
1288 goto err_remove_brightness;
1290 acpi_device_dir(device) = device_dir;
1294 err_remove_brightness:
1295 remove_proc_entry("brightness", device_dir);
1297 remove_proc_entry("state", device_dir);
1299 remove_proc_entry("info", device_dir);
1301 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1305 static int acpi_video_device_remove_fs(struct acpi_device *device)
1307 struct acpi_video_device *vid_dev;
1308 struct proc_dir_entry *device_dir;
1310 vid_dev = acpi_driver_data(device);
1311 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
1314 device_dir = acpi_device_dir(device);
1316 remove_proc_entry("info", device_dir);
1317 remove_proc_entry("state", device_dir);
1318 remove_proc_entry("brightness", device_dir);
1319 remove_proc_entry("EDID", device_dir);
1320 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1321 acpi_device_dir(device) = NULL;
1328 static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
1330 struct acpi_video_bus *video = seq->private;
1336 seq_printf(seq, "Switching heads: %s\n",
1337 video->flags.multihead ? "yes" : "no");
1338 seq_printf(seq, "Video ROM: %s\n",
1339 video->flags.rom ? "yes" : "no");
1340 seq_printf(seq, "Device to be POSTed on boot: %s\n",
1341 video->flags.post ? "yes" : "no");
1347 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
1349 return single_open(file, acpi_video_bus_info_seq_show,
1353 static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
1355 struct acpi_video_bus *video = seq->private;
1361 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
1362 seq_printf(seq, "<TODO>\n");
1368 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
1370 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1373 static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
1375 struct acpi_video_bus *video = seq->private;
1376 unsigned long long options;
1383 status = acpi_video_bus_POST_options(video, &options);
1384 if (ACPI_SUCCESS(status)) {
1385 if (!(options & 1)) {
1386 printk(KERN_WARNING PREFIX
1387 "The motherboard VGA device is not listed as a possible POST device.\n");
1388 printk(KERN_WARNING PREFIX
1389 "This indicates a BIOS bug. Please contact the manufacturer.\n");
1391 printk(KERN_WARNING "%llx\n", options);
1392 seq_printf(seq, "can POST: <integrated video>");
1394 seq_printf(seq, " <PCI video>");
1396 seq_printf(seq, " <AGP video>");
1397 seq_putc(seq, '\n');
1399 seq_printf(seq, "<not supported>\n");
1405 acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
1407 return single_open(file, acpi_video_bus_POST_info_seq_show,
1411 static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
1413 struct acpi_video_bus *video = seq->private;
1415 unsigned long long id;
1421 status = acpi_video_bus_get_POST(video, &id);
1422 if (!ACPI_SUCCESS(status)) {
1423 seq_printf(seq, "<not supported>\n");
1426 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
1432 static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
1434 struct acpi_video_bus *video = seq->private;
1437 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
1442 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
1444 return single_open(file, acpi_video_bus_POST_seq_show,
1448 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
1450 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1454 acpi_video_bus_write_POST(struct file *file,
1455 const char __user * buffer,
1456 size_t count, loff_t * data)
1459 struct seq_file *m = file->private_data;
1460 struct acpi_video_bus *video = m->private;
1461 char str[12] = { 0 };
1462 unsigned long long opt, options;
1465 if (!video || count + 1 > sizeof str)
1468 status = acpi_video_bus_POST_options(video, &options);
1469 if (!ACPI_SUCCESS(status))
1472 if (copy_from_user(str, buffer, count))
1476 opt = strtoul(str, NULL, 0);
1480 /* just in case an OEM 'forgot' the motherboard... */
1483 if (options & (1ul << opt)) {
1484 status = acpi_video_bus_set_POST(video, opt);
1485 if (!ACPI_SUCCESS(status))
1494 acpi_video_bus_write_DOS(struct file *file,
1495 const char __user * buffer,
1496 size_t count, loff_t * data)
1499 struct seq_file *m = file->private_data;
1500 struct acpi_video_bus *video = m->private;
1501 char str[12] = { 0 };
1505 if (!video || count + 1 > sizeof str)
1508 if (copy_from_user(str, buffer, count))
1512 opt = strtoul(str, NULL, 0);
1516 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
1518 if (!ACPI_SUCCESS(status))
1524 static int acpi_video_bus_add_fs(struct acpi_device *device)
1526 struct acpi_video_bus *video = acpi_driver_data(device);
1527 struct proc_dir_entry *device_dir;
1528 struct proc_dir_entry *entry;
1530 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1534 device_dir->owner = THIS_MODULE;
1537 entry = proc_create_data("info", S_IRUGO, device_dir,
1538 &acpi_video_bus_info_fops,
1539 acpi_driver_data(device));
1541 goto err_remove_dir;
1544 entry = proc_create_data("ROM", S_IRUGO, device_dir,
1545 &acpi_video_bus_ROM_fops,
1546 acpi_driver_data(device));
1548 goto err_remove_info;
1550 /* 'POST_info' [R] */
1551 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
1552 &acpi_video_bus_POST_info_fops,
1553 acpi_driver_data(device));
1555 goto err_remove_rom;
1558 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
1560 &acpi_video_bus_POST_fops,
1561 acpi_driver_data(device));
1563 goto err_remove_post_info;
1566 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
1568 &acpi_video_bus_DOS_fops,
1569 acpi_driver_data(device));
1571 goto err_remove_post;
1573 video->dir = acpi_device_dir(device) = device_dir;
1577 remove_proc_entry("POST", device_dir);
1578 err_remove_post_info:
1579 remove_proc_entry("POST_info", device_dir);
1581 remove_proc_entry("ROM", device_dir);
1583 remove_proc_entry("info", device_dir);
1585 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1589 static int acpi_video_bus_remove_fs(struct acpi_device *device)
1591 struct proc_dir_entry *device_dir = acpi_device_dir(device);
1594 remove_proc_entry("info", device_dir);
1595 remove_proc_entry("ROM", device_dir);
1596 remove_proc_entry("POST_info", device_dir);
1597 remove_proc_entry("POST", device_dir);
1598 remove_proc_entry("DOS", device_dir);
1599 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1600 acpi_device_dir(device) = NULL;
1606 /* --------------------------------------------------------------------------
1608 -------------------------------------------------------------------------- */
1610 /* device interface */
1611 static struct acpi_video_device_attrib*
1612 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1614 struct acpi_video_enumerated_device *ids;
1617 for (i = 0; i < video->attached_count; i++) {
1618 ids = &video->attached_array[i];
1619 if ((ids->value.int_val & 0xffff) == device_id)
1620 return &ids->value.attrib;
1627 acpi_video_bus_get_one_device(struct acpi_device *device,
1628 struct acpi_video_bus *video)
1630 unsigned long long device_id;
1632 struct acpi_video_device *data;
1633 struct acpi_video_device_attrib* attribute;
1635 if (!device || !video)
1639 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1640 if (ACPI_SUCCESS(status)) {
1642 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1646 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1647 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1648 device->driver_data = data;
1650 data->device_id = device_id;
1651 data->video = video;
1654 attribute = acpi_video_get_device_attr(video, device_id);
1656 if((attribute != NULL) && attribute->device_id_scheme) {
1657 switch (attribute->display_type) {
1658 case ACPI_VIDEO_DISPLAY_CRT:
1659 data->flags.crt = 1;
1661 case ACPI_VIDEO_DISPLAY_TV:
1662 data->flags.tvout = 1;
1664 case ACPI_VIDEO_DISPLAY_DVI:
1665 data->flags.dvi = 1;
1667 case ACPI_VIDEO_DISPLAY_LCD:
1668 data->flags.lcd = 1;
1671 data->flags.unknown = 1;
1674 if(attribute->bios_can_detect)
1675 data->flags.bios = 1;
1677 data->flags.unknown = 1;
1679 acpi_video_device_bind(video, data);
1680 acpi_video_device_find_cap(data);
1682 status = acpi_install_notify_handler(device->handle,
1684 acpi_video_device_notify,
1686 if (ACPI_FAILURE(status)) {
1687 printk(KERN_ERR PREFIX
1688 "Error installing notify handler\n");
1689 if(data->brightness)
1690 kfree(data->brightness->levels);
1691 kfree(data->brightness);
1696 mutex_lock(&video->device_list_lock);
1697 list_add_tail(&data->entry, &video->video_device_list);
1698 mutex_unlock(&video->device_list_lock);
1700 acpi_video_device_add_fs(device);
1710 * video : video bus device
1715 * Enumerate the video device list of the video bus,
1716 * bind the ids with the corresponding video devices
1717 * under the video bus.
1720 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1722 struct acpi_video_device *dev;
1724 mutex_lock(&video->device_list_lock);
1726 list_for_each_entry(dev, &video->video_device_list, entry)
1727 acpi_video_device_bind(video, dev);
1729 mutex_unlock(&video->device_list_lock);
1734 * video : video bus device
1735 * device : video output device under the video
1741 * Bind the ids with the corresponding video devices
1742 * under the video bus.
1746 acpi_video_device_bind(struct acpi_video_bus *video,
1747 struct acpi_video_device *device)
1749 struct acpi_video_enumerated_device *ids;
1752 for (i = 0; i < video->attached_count; i++) {
1753 ids = &video->attached_array[i];
1754 if (device->device_id == (ids->value.int_val & 0xffff)) {
1755 ids->bind_info = device;
1756 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1763 * video : video bus device
1768 * Call _DOD to enumerate all devices attached to display adapter
1772 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1777 struct acpi_video_enumerated_device *active_list;
1778 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1779 union acpi_object *dod = NULL;
1780 union acpi_object *obj;
1782 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1783 if (!ACPI_SUCCESS(status)) {
1784 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1788 dod = buffer.pointer;
1789 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1790 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1795 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1796 dod->package.count));
1798 active_list = kcalloc(1 + dod->package.count,
1799 sizeof(struct acpi_video_enumerated_device),
1807 for (i = 0; i < dod->package.count; i++) {
1808 obj = &dod->package.elements[i];
1810 if (obj->type != ACPI_TYPE_INTEGER) {
1811 printk(KERN_ERR PREFIX
1812 "Invalid _DOD data in element %d\n", i);
1816 active_list[count].value.int_val = obj->integer.value;
1817 active_list[count].bind_info = NULL;
1818 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1819 (int)obj->integer.value));
1823 kfree(video->attached_array);
1825 video->attached_array = active_list;
1826 video->attached_count = count;
1829 kfree(buffer.pointer);
1834 acpi_video_get_next_level(struct acpi_video_device *device,
1835 u32 level_current, u32 event)
1837 int min, max, min_above, max_below, i, l, delta = 255;
1838 max = max_below = 0;
1839 min = min_above = 255;
1840 /* Find closest level to level_current */
1841 for (i = 2; i < device->brightness->count; i++) {
1842 l = device->brightness->levels[i];
1843 if (abs(l - level_current) < abs(delta)) {
1844 delta = l - level_current;
1849 /* Ajust level_current to closest available level */
1850 level_current += delta;
1851 for (i = 2; i < device->brightness->count; i++) {
1852 l = device->brightness->levels[i];
1857 if (l < min_above && l > level_current)
1859 if (l > max_below && l < level_current)
1864 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1865 return (level_current < max) ? min_above : min;
1866 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1867 return (level_current < max) ? min_above : max;
1868 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1869 return (level_current > min) ? max_below : min;
1870 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1871 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1874 return level_current;
1879 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1881 unsigned long long level_current, level_next;
1882 int result = -EINVAL;
1884 if (!device->brightness)
1887 result = acpi_video_device_lcd_get_level_current(device,
1892 level_next = acpi_video_get_next_level(device, level_current, event);
1894 result = acpi_video_device_lcd_set_level(device, level_next);
1898 printk(KERN_ERR PREFIX "Failed to switch the brightness\n");
1904 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1905 struct acpi_device *device)
1908 struct acpi_device *dev;
1910 acpi_video_device_enumerate(video);
1912 list_for_each_entry(dev, &device->children, node) {
1914 status = acpi_video_bus_get_one_device(dev, video);
1915 if (ACPI_FAILURE(status)) {
1916 printk(KERN_WARNING PREFIX
1917 "Cant attach device");
1924 static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1927 struct acpi_video_bus *video;
1930 if (!device || !device->video)
1933 video = device->video;
1935 acpi_video_device_remove_fs(device->dev);
1937 status = acpi_remove_notify_handler(device->dev->handle,
1939 acpi_video_device_notify);
1940 backlight_device_unregister(device->backlight);
1942 sysfs_remove_link(&device->dev->dev.kobj,
1944 sysfs_remove_link(&device->cdev->device.kobj,
1946 thermal_cooling_device_unregister(device->cdev);
1947 device->cdev = NULL;
1949 video_output_unregister(device->output_dev);
1954 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1957 struct acpi_video_device *dev, *next;
1959 mutex_lock(&video->device_list_lock);
1961 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1963 status = acpi_video_bus_put_one_device(dev);
1964 if (ACPI_FAILURE(status))
1965 printk(KERN_WARNING PREFIX
1966 "hhuuhhuu bug in acpi video driver.\n");
1968 if (dev->brightness) {
1969 kfree(dev->brightness->levels);
1970 kfree(dev->brightness);
1972 list_del(&dev->entry);
1976 mutex_unlock(&video->device_list_lock);
1981 /* acpi_video interface */
1983 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1985 return acpi_video_bus_DOS(video, 0, 0);
1988 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1990 return acpi_video_bus_DOS(video, 0, 1);
1993 static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
1995 struct acpi_video_bus *video = data;
1996 struct acpi_device *device = NULL;
1997 struct input_dev *input;
2003 device = video->device;
2004 input = video->input;
2007 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
2008 * most likely via hotkey. */
2009 acpi_bus_generate_proc_event(device, event, 0);
2010 keycode = KEY_SWITCHVIDEOMODE;
2013 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
2015 acpi_video_device_enumerate(video);
2016 acpi_video_device_rebind(video);
2017 acpi_bus_generate_proc_event(device, event, 0);
2018 keycode = KEY_SWITCHVIDEOMODE;
2021 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
2022 acpi_bus_generate_proc_event(device, event, 0);
2023 keycode = KEY_SWITCHVIDEOMODE;
2025 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
2026 acpi_bus_generate_proc_event(device, event, 0);
2027 keycode = KEY_VIDEO_NEXT;
2029 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
2030 acpi_bus_generate_proc_event(device, event, 0);
2031 keycode = KEY_VIDEO_PREV;
2035 keycode = KEY_UNKNOWN;
2036 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2037 "Unsupported event [0x%x]\n", event));
2041 acpi_notifier_call_chain(device, event, 0);
2042 input_report_key(input, keycode, 1);
2044 input_report_key(input, keycode, 0);
2050 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
2052 struct acpi_video_device *video_device = data;
2053 struct acpi_device *device = NULL;
2054 struct acpi_video_bus *bus;
2055 struct input_dev *input;
2061 device = video_device->dev;
2062 bus = video_device->video;
2066 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
2067 if (brightness_switch_enabled)
2068 acpi_video_switch_brightness(video_device, event);
2069 acpi_bus_generate_proc_event(device, event, 0);
2070 keycode = KEY_BRIGHTNESS_CYCLE;
2072 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
2073 if (brightness_switch_enabled)
2074 acpi_video_switch_brightness(video_device, event);
2075 acpi_bus_generate_proc_event(device, event, 0);
2076 keycode = KEY_BRIGHTNESSUP;
2078 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
2079 if (brightness_switch_enabled)
2080 acpi_video_switch_brightness(video_device, event);
2081 acpi_bus_generate_proc_event(device, event, 0);
2082 keycode = KEY_BRIGHTNESSDOWN;
2084 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
2085 if (brightness_switch_enabled)
2086 acpi_video_switch_brightness(video_device, event);
2087 acpi_bus_generate_proc_event(device, event, 0);
2088 keycode = KEY_BRIGHTNESS_ZERO;
2090 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
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_DISPLAY_OFF;
2097 keycode = KEY_UNKNOWN;
2098 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2099 "Unsupported event [0x%x]\n", event));
2103 acpi_notifier_call_chain(device, event, 0);
2104 input_report_key(input, keycode, 1);
2106 input_report_key(input, keycode, 0);
2112 static int instance;
2113 static int acpi_video_resume(struct acpi_device *device)
2115 struct acpi_video_bus *video;
2116 struct acpi_video_device *video_device;
2119 if (!device || !acpi_driver_data(device))
2122 video = acpi_driver_data(device);
2124 for (i = 0; i < video->attached_count; i++) {
2125 video_device = video->attached_array[i].bind_info;
2126 if (video_device && video_device->backlight)
2127 acpi_video_set_brightness(video_device->backlight);
2132 static int acpi_video_bus_add(struct acpi_device *device)
2135 struct acpi_video_bus *video;
2136 struct input_dev *input;
2139 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
2143 /* a hack to fix the duplicate name "VID" problem on T61 */
2144 if (!strcmp(device->pnp.bus_id, "VID")) {
2146 device->pnp.bus_id[3] = '0' + instance;
2149 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2150 if (!strcmp(device->pnp.bus_id, "VGA")) {
2152 device->pnp.bus_id[3] = '0' + instance;
2156 video->device = device;
2157 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2158 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
2159 device->driver_data = video;
2161 acpi_video_bus_find_cap(video);
2162 error = acpi_video_bus_check(video);
2164 goto err_free_video;
2166 error = acpi_video_bus_add_fs(device);
2168 goto err_free_video;
2170 mutex_init(&video->device_list_lock);
2171 INIT_LIST_HEAD(&video->video_device_list);
2173 acpi_video_bus_get_devices(video, device);
2174 acpi_video_bus_start_devices(video);
2176 status = acpi_install_notify_handler(device->handle,
2178 acpi_video_bus_notify, video);
2179 if (ACPI_FAILURE(status)) {
2180 printk(KERN_ERR PREFIX
2181 "Error installing notify handler\n");
2183 goto err_stop_video;
2186 video->input = input = input_allocate_device();
2189 goto err_uninstall_notify;
2192 snprintf(video->phys, sizeof(video->phys),
2193 "%s/video/input0", acpi_device_hid(video->device));
2195 input->name = acpi_device_name(video->device);
2196 input->phys = video->phys;
2197 input->id.bustype = BUS_HOST;
2198 input->id.product = 0x06;
2199 input->dev.parent = &device->dev;
2200 input->evbit[0] = BIT(EV_KEY);
2201 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2202 set_bit(KEY_VIDEO_NEXT, input->keybit);
2203 set_bit(KEY_VIDEO_PREV, input->keybit);
2204 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2205 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2206 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2207 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2208 set_bit(KEY_DISPLAY_OFF, input->keybit);
2209 set_bit(KEY_UNKNOWN, input->keybit);
2211 error = input_register_device(input);
2213 goto err_free_input_dev;
2215 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
2216 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2217 video->flags.multihead ? "yes" : "no",
2218 video->flags.rom ? "yes" : "no",
2219 video->flags.post ? "yes" : "no");
2224 input_free_device(input);
2225 err_uninstall_notify:
2226 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
2227 acpi_video_bus_notify);
2229 acpi_video_bus_stop_devices(video);
2230 acpi_video_bus_put_devices(video);
2231 kfree(video->attached_array);
2232 acpi_video_bus_remove_fs(device);
2235 device->driver_data = NULL;
2240 static int acpi_video_bus_remove(struct acpi_device *device, int type)
2242 acpi_status status = 0;
2243 struct acpi_video_bus *video = NULL;
2246 if (!device || !acpi_driver_data(device))
2249 video = acpi_driver_data(device);
2251 acpi_video_bus_stop_devices(video);
2253 status = acpi_remove_notify_handler(video->device->handle,
2255 acpi_video_bus_notify);
2257 acpi_video_bus_put_devices(video);
2258 acpi_video_bus_remove_fs(device);
2260 input_unregister_device(video->input);
2261 kfree(video->attached_array);
2267 static int __init intel_opregion_present(void)
2269 #if defined(CONFIG_DRM_I915) || defined(CONFIG_DRM_I915_MODULE)
2270 struct pci_dev *dev = NULL;
2273 for_each_pci_dev(dev) {
2274 if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
2276 if (dev->vendor != PCI_VENDOR_ID_INTEL)
2278 pci_read_config_dword(dev, 0xfc, &address);
2287 int acpi_video_register(void)
2291 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2292 if (!acpi_video_dir)
2294 acpi_video_dir->owner = THIS_MODULE;
2296 result = acpi_bus_register_driver(&acpi_video_bus);
2298 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2304 EXPORT_SYMBOL(acpi_video_register);
2307 * This is kind of nasty. Hardware using Intel chipsets may require
2308 * the video opregion code to be run first in order to initialise
2309 * state before any ACPI video calls are made. To handle this we defer
2310 * registration of the video class until the opregion code has run.
2313 static int __init acpi_video_init(void)
2315 if (intel_opregion_present())
2318 return acpi_video_register();
2321 static void __exit acpi_video_exit(void)
2324 acpi_bus_unregister_driver(&acpi_video_bus);
2326 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2331 module_init(acpi_video_init);
2332 module_exit(acpi_video_exit);