ACPI: constify VFTs (2/2)
[linux-2.6/kvm.git] / drivers / acpi / video.c
blob52779549d367b037e47c4089ac89c158490f858a
1 /*
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 <asm/uaccess.h>
42 #include <acpi/acpi_bus.h>
43 #include <acpi/acpi_drivers.h>
45 #define ACPI_VIDEO_CLASS "video"
46 #define ACPI_VIDEO_BUS_NAME "Video Bus"
47 #define ACPI_VIDEO_DEVICE_NAME "Video Device"
48 #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
49 #define ACPI_VIDEO_NOTIFY_PROBE 0x81
50 #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
51 #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
52 #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
54 #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
55 #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
56 #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
57 #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
58 #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
60 #define MAX_NAME_LEN 20
62 #define ACPI_VIDEO_DISPLAY_CRT 1
63 #define ACPI_VIDEO_DISPLAY_TV 2
64 #define ACPI_VIDEO_DISPLAY_DVI 3
65 #define ACPI_VIDEO_DISPLAY_LCD 4
67 #define _COMPONENT ACPI_VIDEO_COMPONENT
68 ACPI_MODULE_NAME("video");
70 MODULE_AUTHOR("Bruno Ducrot");
71 MODULE_DESCRIPTION("ACPI Video Driver");
72 MODULE_LICENSE("GPL");
74 static int brightness_switch_enabled = 1;
75 module_param(brightness_switch_enabled, bool, 0644);
77 static int acpi_video_bus_add(struct acpi_device *device);
78 static int acpi_video_bus_remove(struct acpi_device *device, int type);
79 static int acpi_video_resume(struct acpi_device *device);
81 static const struct acpi_device_id video_device_ids[] = {
82 {ACPI_VIDEO_HID, 0},
83 {"", 0},
85 MODULE_DEVICE_TABLE(acpi, video_device_ids);
87 static struct acpi_driver acpi_video_bus = {
88 .name = "video",
89 .class = ACPI_VIDEO_CLASS,
90 .ids = video_device_ids,
91 .ops = {
92 .add = acpi_video_bus_add,
93 .remove = acpi_video_bus_remove,
94 .resume = acpi_video_resume,
98 struct acpi_video_bus_flags {
99 u8 multihead:1; /* can switch video heads */
100 u8 rom:1; /* can retrieve a video rom */
101 u8 post:1; /* can configure the head to */
102 u8 reserved:5;
105 struct acpi_video_bus_cap {
106 u8 _DOS:1; /*Enable/Disable output switching */
107 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
108 u8 _ROM:1; /*Get ROM Data */
109 u8 _GPD:1; /*Get POST Device */
110 u8 _SPD:1; /*Set POST Device */
111 u8 _VPO:1; /*Video POST Options */
112 u8 reserved:2;
115 struct acpi_video_device_attrib {
116 u32 display_index:4; /* A zero-based instance of the Display */
117 u32 display_port_attachment:4; /*This field differentiates the display type */
118 u32 display_type:4; /*Describe the specific type in use */
119 u32 vendor_specific:4; /*Chipset Vendor Specific */
120 u32 bios_can_detect:1; /*BIOS can detect the device */
121 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
122 the VGA device. */
123 u32 pipe_id:3; /*For VGA multiple-head devices. */
124 u32 reserved:10; /*Must be 0 */
125 u32 device_id_scheme:1; /*Device ID Scheme */
128 struct acpi_video_enumerated_device {
129 union {
130 u32 int_val;
131 struct acpi_video_device_attrib attrib;
132 } value;
133 struct acpi_video_device *bind_info;
136 struct acpi_video_bus {
137 struct acpi_device *device;
138 u8 dos_setting;
139 struct acpi_video_enumerated_device *attached_array;
140 u8 attached_count;
141 struct acpi_video_bus_cap cap;
142 struct acpi_video_bus_flags flags;
143 struct list_head video_device_list;
144 struct mutex device_list_lock; /* protects video_device_list */
145 struct proc_dir_entry *dir;
146 struct input_dev *input;
147 char phys[32]; /* for input device */
150 struct acpi_video_device_flags {
151 u8 crt:1;
152 u8 lcd:1;
153 u8 tvout:1;
154 u8 dvi:1;
155 u8 bios:1;
156 u8 unknown:1;
157 u8 reserved:2;
160 struct acpi_video_device_cap {
161 u8 _ADR:1; /*Return the unique ID */
162 u8 _BCL:1; /*Query list of brightness control levels supported */
163 u8 _BCM:1; /*Set the brightness level */
164 u8 _BQC:1; /* Get current brightness level */
165 u8 _DDC:1; /*Return the EDID for this device */
166 u8 _DCS:1; /*Return status of output device */
167 u8 _DGS:1; /*Query graphics state */
168 u8 _DSS:1; /*Device state set */
171 struct acpi_video_device_brightness {
172 int curr;
173 int count;
174 int *levels;
177 struct acpi_video_device {
178 unsigned long device_id;
179 struct acpi_video_device_flags flags;
180 struct acpi_video_device_cap cap;
181 struct list_head entry;
182 struct acpi_video_bus *video;
183 struct acpi_device *dev;
184 struct acpi_video_device_brightness *brightness;
185 struct backlight_device *backlight;
186 struct thermal_cooling_device *cdev;
187 struct output_device *output_dev;
190 /* bus */
191 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
192 static const struct file_operations acpi_video_bus_info_fops = {
193 .owner = THIS_MODULE,
194 .open = acpi_video_bus_info_open_fs,
195 .read = seq_read,
196 .llseek = seq_lseek,
197 .release = single_release,
200 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
201 static const struct file_operations acpi_video_bus_ROM_fops = {
202 .owner = THIS_MODULE,
203 .open = acpi_video_bus_ROM_open_fs,
204 .read = seq_read,
205 .llseek = seq_lseek,
206 .release = single_release,
209 static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
210 struct file *file);
211 static const struct file_operations acpi_video_bus_POST_info_fops = {
212 .owner = THIS_MODULE,
213 .open = acpi_video_bus_POST_info_open_fs,
214 .read = seq_read,
215 .llseek = seq_lseek,
216 .release = single_release,
219 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
220 static int acpi_video_bus_write_POST(struct file *file,
221 const char __user *buffer, size_t count, loff_t *data);
222 static const struct file_operations acpi_video_bus_POST_fops = {
223 .owner = THIS_MODULE,
224 .open = acpi_video_bus_POST_open_fs,
225 .read = seq_read,
226 .write = acpi_video_bus_write_POST,
227 .llseek = seq_lseek,
228 .release = single_release,
231 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
232 static int acpi_video_bus_write_DOS(struct file *file,
233 const char __user *buffer, size_t count, loff_t *data);
234 static const struct file_operations acpi_video_bus_DOS_fops = {
235 .owner = THIS_MODULE,
236 .open = acpi_video_bus_DOS_open_fs,
237 .read = seq_read,
238 .write = acpi_video_bus_write_DOS,
239 .llseek = seq_lseek,
240 .release = single_release,
243 /* device */
244 static int acpi_video_device_info_open_fs(struct inode *inode,
245 struct file *file);
246 static const struct file_operations acpi_video_device_info_fops = {
247 .owner = THIS_MODULE,
248 .open = acpi_video_device_info_open_fs,
249 .read = seq_read,
250 .llseek = seq_lseek,
251 .release = single_release,
254 static int acpi_video_device_state_open_fs(struct inode *inode,
255 struct file *file);
256 static int acpi_video_device_write_state(struct file *file,
257 const char __user *buffer, size_t count, loff_t *data);
258 static const struct file_operations acpi_video_device_state_fops = {
259 .owner = THIS_MODULE,
260 .open = acpi_video_device_state_open_fs,
261 .read = seq_read,
262 .write = acpi_video_device_write_state,
263 .llseek = seq_lseek,
264 .release = single_release,
267 static int acpi_video_device_brightness_open_fs(struct inode *inode,
268 struct file *file);
269 static int acpi_video_device_write_brightness(struct file *file,
270 const char __user *buffer, size_t count, loff_t *data);
271 static struct file_operations acpi_video_device_brightness_fops = {
272 .owner = THIS_MODULE,
273 .open = acpi_video_device_brightness_open_fs,
274 .read = seq_read,
275 .write = acpi_video_device_write_brightness,
276 .llseek = seq_lseek,
277 .release = single_release,
280 static int acpi_video_device_EDID_open_fs(struct inode *inode,
281 struct file *file);
282 static const struct file_operations acpi_video_device_EDID_fops = {
283 .owner = THIS_MODULE,
284 .open = acpi_video_device_EDID_open_fs,
285 .read = seq_read,
286 .llseek = seq_lseek,
287 .release = single_release,
290 static const char device_decode[][30] = {
291 "motherboard VGA device",
292 "PCI VGA device",
293 "AGP VGA device",
294 "UNKNOWN",
297 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
298 static void acpi_video_device_rebind(struct acpi_video_bus *video);
299 static void acpi_video_device_bind(struct acpi_video_bus *video,
300 struct acpi_video_device *device);
301 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
302 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
303 int level);
304 static int acpi_video_device_lcd_get_level_current(
305 struct acpi_video_device *device,
306 unsigned long long *level);
307 static int acpi_video_get_next_level(struct acpi_video_device *device,
308 u32 level_current, u32 event);
309 static void acpi_video_switch_brightness(struct acpi_video_device *device,
310 int event);
311 static int acpi_video_device_get_state(struct acpi_video_device *device,
312 unsigned long long *state);
313 static int acpi_video_output_get(struct output_device *od);
314 static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
316 /*backlight device sysfs support*/
317 static int acpi_video_get_brightness(struct backlight_device *bd)
319 unsigned long long cur_level;
320 int i;
321 struct acpi_video_device *vd =
322 (struct acpi_video_device *)bl_get_data(bd);
323 acpi_video_device_lcd_get_level_current(vd, &cur_level);
324 for (i = 2; i < vd->brightness->count; i++) {
325 if (vd->brightness->levels[i] == cur_level)
326 /* The first two entries are special - see page 575
327 of the ACPI spec 3.0 */
328 return i-2;
330 return 0;
333 static int acpi_video_set_brightness(struct backlight_device *bd)
335 int request_level = bd->props.brightness+2;
336 struct acpi_video_device *vd =
337 (struct acpi_video_device *)bl_get_data(bd);
338 acpi_video_device_lcd_set_level(vd,
339 vd->brightness->levels[request_level]);
340 return 0;
343 static struct backlight_ops acpi_backlight_ops = {
344 .get_brightness = acpi_video_get_brightness,
345 .update_status = acpi_video_set_brightness,
348 /*video output device sysfs support*/
349 static int acpi_video_output_get(struct output_device *od)
351 unsigned long long state;
352 struct acpi_video_device *vd =
353 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
354 acpi_video_device_get_state(vd, &state);
355 return (int)state;
358 static int acpi_video_output_set(struct output_device *od)
360 unsigned long state = od->request_state;
361 struct acpi_video_device *vd=
362 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
363 return acpi_video_device_set_state(vd, state);
366 static struct output_properties acpi_output_properties = {
367 .set_state = acpi_video_output_set,
368 .get_status = acpi_video_output_get,
372 /* thermal cooling device callbacks */
373 static int video_get_max_state(struct thermal_cooling_device *cdev, char *buf)
375 struct acpi_device *device = cdev->devdata;
376 struct acpi_video_device *video = acpi_driver_data(device);
378 return sprintf(buf, "%d\n", video->brightness->count - 3);
381 static int video_get_cur_state(struct thermal_cooling_device *cdev, char *buf)
383 struct acpi_device *device = cdev->devdata;
384 struct acpi_video_device *video = acpi_driver_data(device);
385 unsigned long long level;
386 int state;
388 acpi_video_device_lcd_get_level_current(video, &level);
389 for (state = 2; state < video->brightness->count; state++)
390 if (level == video->brightness->levels[state])
391 return sprintf(buf, "%d\n",
392 video->brightness->count - state - 1);
394 return -EINVAL;
397 static int
398 video_set_cur_state(struct thermal_cooling_device *cdev, unsigned int state)
400 struct acpi_device *device = cdev->devdata;
401 struct acpi_video_device *video = acpi_driver_data(device);
402 int level;
404 if ( state >= video->brightness->count - 2)
405 return -EINVAL;
407 state = video->brightness->count - state;
408 level = video->brightness->levels[state -1];
409 return acpi_video_device_lcd_set_level(video, level);
412 static struct thermal_cooling_device_ops video_cooling_ops = {
413 .get_max_state = video_get_max_state,
414 .get_cur_state = video_get_cur_state,
415 .set_cur_state = video_set_cur_state,
418 /* --------------------------------------------------------------------------
419 Video Management
420 -------------------------------------------------------------------------- */
422 /* device */
424 static int
425 acpi_video_device_query(struct acpi_video_device *device, unsigned long long *state)
427 int status;
429 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
431 return status;
434 static int
435 acpi_video_device_get_state(struct acpi_video_device *device,
436 unsigned long long *state)
438 int status;
440 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
442 return status;
445 static int
446 acpi_video_device_set_state(struct acpi_video_device *device, int state)
448 int status;
449 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
450 struct acpi_object_list args = { 1, &arg0 };
451 unsigned long long ret;
454 arg0.integer.value = state;
455 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
457 return status;
460 static int
461 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
462 union acpi_object **levels)
464 int status;
465 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
466 union acpi_object *obj;
469 *levels = NULL;
471 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
472 if (!ACPI_SUCCESS(status))
473 return status;
474 obj = (union acpi_object *)buffer.pointer;
475 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
476 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
477 status = -EFAULT;
478 goto err;
481 *levels = obj;
483 return 0;
485 err:
486 kfree(buffer.pointer);
488 return status;
491 static int
492 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
494 int status = AE_OK;
495 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
496 struct acpi_object_list args = { 1, &arg0 };
497 int state;
500 arg0.integer.value = level;
502 if (device->cap._BCM)
503 status = acpi_evaluate_object(device->dev->handle, "_BCM",
504 &args, NULL);
505 device->brightness->curr = level;
506 for (state = 2; state < device->brightness->count; state++)
507 if (level == device->brightness->levels[state])
508 device->backlight->props.brightness = state - 2;
510 return status;
513 static int
514 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
515 unsigned long long *level)
517 if (device->cap._BQC)
518 return acpi_evaluate_integer(device->dev->handle, "_BQC", NULL,
519 level);
520 *level = device->brightness->curr;
521 return AE_OK;
524 static int
525 acpi_video_device_EDID(struct acpi_video_device *device,
526 union acpi_object **edid, ssize_t length)
528 int status;
529 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
530 union acpi_object *obj;
531 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
532 struct acpi_object_list args = { 1, &arg0 };
535 *edid = NULL;
537 if (!device)
538 return -ENODEV;
539 if (length == 128)
540 arg0.integer.value = 1;
541 else if (length == 256)
542 arg0.integer.value = 2;
543 else
544 return -EINVAL;
546 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
547 if (ACPI_FAILURE(status))
548 return -ENODEV;
550 obj = buffer.pointer;
552 if (obj && obj->type == ACPI_TYPE_BUFFER)
553 *edid = obj;
554 else {
555 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
556 status = -EFAULT;
557 kfree(obj);
560 return status;
563 /* bus */
565 static int
566 acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
568 int status;
569 unsigned long long tmp;
570 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
571 struct acpi_object_list args = { 1, &arg0 };
574 arg0.integer.value = option;
576 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
577 if (ACPI_SUCCESS(status))
578 status = tmp ? (-EINVAL) : (AE_OK);
580 return status;
583 static int
584 acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long long *id)
586 int status;
588 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
590 return status;
593 static int
594 acpi_video_bus_POST_options(struct acpi_video_bus *video,
595 unsigned long long *options)
597 int status;
599 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
600 *options &= 3;
602 return status;
606 * Arg:
607 * video : video bus device pointer
608 * bios_flag :
609 * 0. The system BIOS should NOT automatically switch(toggle)
610 * the active display output.
611 * 1. The system BIOS should automatically switch (toggle) the
612 * active display output. No switch event.
613 * 2. The _DGS value should be locked.
614 * 3. The system BIOS should not automatically switch (toggle) the
615 * active display output, but instead generate the display switch
616 * event notify code.
617 * lcd_flag :
618 * 0. The system BIOS should automatically control the brightness level
619 * of the LCD when the power changes from AC to DC
620 * 1. The system BIOS should NOT automatically control the brightness
621 * level of the LCD when the power changes from AC to DC.
622 * Return Value:
623 * -1 wrong arg.
626 static int
627 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
629 acpi_integer status = 0;
630 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
631 struct acpi_object_list args = { 1, &arg0 };
634 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
635 status = -1;
636 goto Failed;
638 arg0.integer.value = (lcd_flag << 2) | bios_flag;
639 video->dos_setting = arg0.integer.value;
640 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
642 Failed:
643 return status;
647 * Simple comparison function used to sort backlight levels.
650 static int
651 acpi_video_cmp_level(const void *a, const void *b)
653 return *(int *)a - *(int *)b;
657 * Arg:
658 * device : video output device (LCD, CRT, ..)
660 * Return Value:
661 * Maximum brightness level
663 * Allocate and initialize device->brightness.
666 static int
667 acpi_video_init_brightness(struct acpi_video_device *device)
669 union acpi_object *obj = NULL;
670 int i, max_level = 0, count = 0;
671 union acpi_object *o;
672 struct acpi_video_device_brightness *br = NULL;
674 if (!ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
675 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available "
676 "LCD brightness level\n"));
677 goto out;
680 if (obj->package.count < 2)
681 goto out;
683 br = kzalloc(sizeof(*br), GFP_KERNEL);
684 if (!br) {
685 printk(KERN_ERR "can't allocate memory\n");
686 goto out;
689 br->levels = kmalloc(obj->package.count * sizeof *(br->levels),
690 GFP_KERNEL);
691 if (!br->levels)
692 goto out_free;
694 for (i = 0; i < obj->package.count; i++) {
695 o = (union acpi_object *)&obj->package.elements[i];
696 if (o->type != ACPI_TYPE_INTEGER) {
697 printk(KERN_ERR PREFIX "Invalid data\n");
698 continue;
700 br->levels[count] = (u32) o->integer.value;
702 if (br->levels[count] > max_level)
703 max_level = br->levels[count];
704 count++;
707 /* don't sort the first two brightness levels */
708 sort(&br->levels[2], count - 2, sizeof(br->levels[2]),
709 acpi_video_cmp_level, NULL);
711 if (count < 2)
712 goto out_free_levels;
714 br->count = count;
715 device->brightness = br;
716 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "found %d brightness levels\n", count));
717 kfree(obj);
718 return max_level;
720 out_free_levels:
721 kfree(br->levels);
722 out_free:
723 kfree(br);
724 out:
725 device->brightness = NULL;
726 kfree(obj);
727 return 0;
731 * Arg:
732 * device : video output device (LCD, CRT, ..)
734 * Return Value:
735 * None
737 * Find out all required AML methods defined under the output
738 * device.
741 static void acpi_video_device_find_cap(struct acpi_video_device *device)
743 acpi_handle h_dummy1;
744 u32 max_level = 0;
747 memset(&device->cap, 0, sizeof(device->cap));
749 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
750 device->cap._ADR = 1;
752 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
753 device->cap._BCL = 1;
755 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
756 device->cap._BCM = 1;
758 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
759 device->cap._BQC = 1;
760 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
761 device->cap._DDC = 1;
763 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
764 device->cap._DCS = 1;
766 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
767 device->cap._DGS = 1;
769 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
770 device->cap._DSS = 1;
773 if (acpi_video_backlight_support())
774 max_level = acpi_video_init_brightness(device);
776 if (device->cap._BCL && device->cap._BCM && max_level > 0) {
777 int result;
778 static int count = 0;
779 char *name;
780 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
781 if (!name)
782 return;
784 sprintf(name, "acpi_video%d", count++);
785 device->backlight = backlight_device_register(name,
786 NULL, device, &acpi_backlight_ops);
787 device->backlight->props.max_brightness = device->brightness->count-3;
789 * If there exists the _BQC object, the _BQC object will be
790 * called to get the current backlight brightness. Otherwise
791 * the brightness will be set to the maximum.
793 if (device->cap._BQC)
794 device->backlight->props.brightness =
795 acpi_video_get_brightness(device->backlight);
796 else
797 device->backlight->props.brightness =
798 device->backlight->props.max_brightness;
799 backlight_update_status(device->backlight);
800 kfree(name);
802 device->cdev = thermal_cooling_device_register("LCD",
803 device->dev, &video_cooling_ops);
804 if (IS_ERR(device->cdev))
805 return;
807 dev_info(&device->dev->dev, "registered as cooling_device%d\n",
808 device->cdev->id);
809 result = sysfs_create_link(&device->dev->dev.kobj,
810 &device->cdev->device.kobj,
811 "thermal_cooling");
812 if (result)
813 printk(KERN_ERR PREFIX "Create sysfs link\n");
814 result = sysfs_create_link(&device->cdev->device.kobj,
815 &device->dev->dev.kobj, "device");
816 if (result)
817 printk(KERN_ERR PREFIX "Create sysfs link\n");
821 if (acpi_video_display_switch_support()) {
823 if (device->cap._DCS && device->cap._DSS) {
824 static int count;
825 char *name;
826 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
827 if (!name)
828 return;
829 sprintf(name, "acpi_video%d", count++);
830 device->output_dev = video_output_register(name,
831 NULL, device, &acpi_output_properties);
832 kfree(name);
838 * Arg:
839 * device : video output device (VGA)
841 * Return Value:
842 * None
844 * Find out all required AML methods defined under the video bus device.
847 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
849 acpi_handle h_dummy1;
851 memset(&video->cap, 0, sizeof(video->cap));
852 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
853 video->cap._DOS = 1;
855 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
856 video->cap._DOD = 1;
858 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
859 video->cap._ROM = 1;
861 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
862 video->cap._GPD = 1;
864 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
865 video->cap._SPD = 1;
867 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
868 video->cap._VPO = 1;
873 * Check whether the video bus device has required AML method to
874 * support the desired features
877 static int acpi_video_bus_check(struct acpi_video_bus *video)
879 acpi_status status = -ENOENT;
880 struct device *dev;
882 if (!video)
883 return -EINVAL;
885 dev = acpi_get_physical_pci_device(video->device->handle);
886 if (!dev)
887 return -ENODEV;
888 put_device(dev);
890 /* Since there is no HID, CID and so on for VGA driver, we have
891 * to check well known required nodes.
894 /* Does this device support video switching? */
895 if (video->cap._DOS) {
896 video->flags.multihead = 1;
897 status = 0;
900 /* Does this device support retrieving a video ROM? */
901 if (video->cap._ROM) {
902 video->flags.rom = 1;
903 status = 0;
906 /* Does this device support configuring which video device to POST? */
907 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
908 video->flags.post = 1;
909 status = 0;
912 return status;
915 /* --------------------------------------------------------------------------
916 FS Interface (/proc)
917 -------------------------------------------------------------------------- */
919 static struct proc_dir_entry *acpi_video_dir;
921 /* video devices */
923 static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
925 struct acpi_video_device *dev = seq->private;
928 if (!dev)
929 goto end;
931 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
932 seq_printf(seq, "type: ");
933 if (dev->flags.crt)
934 seq_printf(seq, "CRT\n");
935 else if (dev->flags.lcd)
936 seq_printf(seq, "LCD\n");
937 else if (dev->flags.tvout)
938 seq_printf(seq, "TVOUT\n");
939 else if (dev->flags.dvi)
940 seq_printf(seq, "DVI\n");
941 else
942 seq_printf(seq, "UNKNOWN\n");
944 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
946 end:
947 return 0;
950 static int
951 acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
953 return single_open(file, acpi_video_device_info_seq_show,
954 PDE(inode)->data);
957 static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
959 int status;
960 struct acpi_video_device *dev = seq->private;
961 unsigned long long state;
964 if (!dev)
965 goto end;
967 status = acpi_video_device_get_state(dev, &state);
968 seq_printf(seq, "state: ");
969 if (ACPI_SUCCESS(status))
970 seq_printf(seq, "0x%02llx\n", state);
971 else
972 seq_printf(seq, "<not supported>\n");
974 status = acpi_video_device_query(dev, &state);
975 seq_printf(seq, "query: ");
976 if (ACPI_SUCCESS(status))
977 seq_printf(seq, "0x%02llx\n", state);
978 else
979 seq_printf(seq, "<not supported>\n");
981 end:
982 return 0;
985 static int
986 acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
988 return single_open(file, acpi_video_device_state_seq_show,
989 PDE(inode)->data);
992 static ssize_t
993 acpi_video_device_write_state(struct file *file,
994 const char __user * buffer,
995 size_t count, loff_t * data)
997 int status;
998 struct seq_file *m = file->private_data;
999 struct acpi_video_device *dev = m->private;
1000 char str[12] = { 0 };
1001 u32 state = 0;
1004 if (!dev || count + 1 > sizeof str)
1005 return -EINVAL;
1007 if (copy_from_user(str, buffer, count))
1008 return -EFAULT;
1010 str[count] = 0;
1011 state = simple_strtoul(str, NULL, 0);
1012 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
1014 status = acpi_video_device_set_state(dev, state);
1016 if (status)
1017 return -EFAULT;
1019 return count;
1022 static int
1023 acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
1025 struct acpi_video_device *dev = seq->private;
1026 int i;
1029 if (!dev || !dev->brightness) {
1030 seq_printf(seq, "<not supported>\n");
1031 return 0;
1034 seq_printf(seq, "levels: ");
1035 for (i = 2; i < dev->brightness->count; i++)
1036 seq_printf(seq, " %d", dev->brightness->levels[i]);
1037 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
1039 return 0;
1042 static int
1043 acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
1045 return single_open(file, acpi_video_device_brightness_seq_show,
1046 PDE(inode)->data);
1049 static ssize_t
1050 acpi_video_device_write_brightness(struct file *file,
1051 const char __user * buffer,
1052 size_t count, loff_t * data)
1054 struct seq_file *m = file->private_data;
1055 struct acpi_video_device *dev = m->private;
1056 char str[5] = { 0 };
1057 unsigned int level = 0;
1058 int i;
1061 if (!dev || !dev->brightness || count + 1 > sizeof str)
1062 return -EINVAL;
1064 if (copy_from_user(str, buffer, count))
1065 return -EFAULT;
1067 str[count] = 0;
1068 level = simple_strtoul(str, NULL, 0);
1070 if (level > 100)
1071 return -EFAULT;
1073 /* validate through the list of available levels */
1074 for (i = 2; i < dev->brightness->count; i++)
1075 if (level == dev->brightness->levels[i]) {
1076 if (ACPI_SUCCESS
1077 (acpi_video_device_lcd_set_level(dev, level)))
1078 dev->brightness->curr = level;
1079 break;
1082 return count;
1085 static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
1087 struct acpi_video_device *dev = seq->private;
1088 int status;
1089 int i;
1090 union acpi_object *edid = NULL;
1093 if (!dev)
1094 goto out;
1096 status = acpi_video_device_EDID(dev, &edid, 128);
1097 if (ACPI_FAILURE(status)) {
1098 status = acpi_video_device_EDID(dev, &edid, 256);
1101 if (ACPI_FAILURE(status)) {
1102 goto out;
1105 if (edid && edid->type == ACPI_TYPE_BUFFER) {
1106 for (i = 0; i < edid->buffer.length; i++)
1107 seq_putc(seq, edid->buffer.pointer[i]);
1110 out:
1111 if (!edid)
1112 seq_printf(seq, "<not supported>\n");
1113 else
1114 kfree(edid);
1116 return 0;
1119 static int
1120 acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
1122 return single_open(file, acpi_video_device_EDID_seq_show,
1123 PDE(inode)->data);
1126 static int acpi_video_device_add_fs(struct acpi_device *device)
1128 struct proc_dir_entry *entry, *device_dir;
1129 struct acpi_video_device *vid_dev;
1131 vid_dev = acpi_driver_data(device);
1132 if (!vid_dev)
1133 return -ENODEV;
1135 device_dir = proc_mkdir(acpi_device_bid(device),
1136 vid_dev->video->dir);
1137 if (!device_dir)
1138 return -ENOMEM;
1140 device_dir->owner = THIS_MODULE;
1142 /* 'info' [R] */
1143 entry = proc_create_data("info", S_IRUGO, device_dir,
1144 &acpi_video_device_info_fops, acpi_driver_data(device));
1145 if (!entry)
1146 goto err_remove_dir;
1148 /* 'state' [R/W] */
1149 entry = proc_create_data("state", S_IFREG | S_IRUGO | S_IWUSR,
1150 device_dir,
1151 &acpi_video_device_state_fops,
1152 acpi_driver_data(device));
1153 if (!entry)
1154 goto err_remove_info;
1156 /* 'brightness' [R/W] */
1157 entry = proc_create_data("brightness", S_IFREG | S_IRUGO | S_IWUSR,
1158 device_dir,
1159 &acpi_video_device_brightness_fops,
1160 acpi_driver_data(device));
1161 if (!entry)
1162 goto err_remove_state;
1164 /* 'EDID' [R] */
1165 entry = proc_create_data("EDID", S_IRUGO, device_dir,
1166 &acpi_video_device_EDID_fops,
1167 acpi_driver_data(device));
1168 if (!entry)
1169 goto err_remove_brightness;
1171 acpi_device_dir(device) = device_dir;
1173 return 0;
1175 err_remove_brightness:
1176 remove_proc_entry("brightness", device_dir);
1177 err_remove_state:
1178 remove_proc_entry("state", device_dir);
1179 err_remove_info:
1180 remove_proc_entry("info", device_dir);
1181 err_remove_dir:
1182 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1183 return -ENOMEM;
1186 static int acpi_video_device_remove_fs(struct acpi_device *device)
1188 struct acpi_video_device *vid_dev;
1189 struct proc_dir_entry *device_dir;
1191 vid_dev = acpi_driver_data(device);
1192 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
1193 return -ENODEV;
1195 device_dir = acpi_device_dir(device);
1196 if (device_dir) {
1197 remove_proc_entry("info", device_dir);
1198 remove_proc_entry("state", device_dir);
1199 remove_proc_entry("brightness", device_dir);
1200 remove_proc_entry("EDID", device_dir);
1201 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1202 acpi_device_dir(device) = NULL;
1205 return 0;
1208 /* video bus */
1209 static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
1211 struct acpi_video_bus *video = seq->private;
1214 if (!video)
1215 goto end;
1217 seq_printf(seq, "Switching heads: %s\n",
1218 video->flags.multihead ? "yes" : "no");
1219 seq_printf(seq, "Video ROM: %s\n",
1220 video->flags.rom ? "yes" : "no");
1221 seq_printf(seq, "Device to be POSTed on boot: %s\n",
1222 video->flags.post ? "yes" : "no");
1224 end:
1225 return 0;
1228 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
1230 return single_open(file, acpi_video_bus_info_seq_show,
1231 PDE(inode)->data);
1234 static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
1236 struct acpi_video_bus *video = seq->private;
1239 if (!video)
1240 goto end;
1242 printk(KERN_INFO PREFIX "Please implement %s\n", __func__);
1243 seq_printf(seq, "<TODO>\n");
1245 end:
1246 return 0;
1249 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
1251 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1254 static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
1256 struct acpi_video_bus *video = seq->private;
1257 unsigned long long options;
1258 int status;
1261 if (!video)
1262 goto end;
1264 status = acpi_video_bus_POST_options(video, &options);
1265 if (ACPI_SUCCESS(status)) {
1266 if (!(options & 1)) {
1267 printk(KERN_WARNING PREFIX
1268 "The motherboard VGA device is not listed as a possible POST device.\n");
1269 printk(KERN_WARNING PREFIX
1270 "This indicates a BIOS bug. Please contact the manufacturer.\n");
1272 printk(KERN_WARNING "%llx\n", options);
1273 seq_printf(seq, "can POST: <integrated video>");
1274 if (options & 2)
1275 seq_printf(seq, " <PCI video>");
1276 if (options & 4)
1277 seq_printf(seq, " <AGP video>");
1278 seq_putc(seq, '\n');
1279 } else
1280 seq_printf(seq, "<not supported>\n");
1281 end:
1282 return 0;
1285 static int
1286 acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
1288 return single_open(file, acpi_video_bus_POST_info_seq_show,
1289 PDE(inode)->data);
1292 static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
1294 struct acpi_video_bus *video = seq->private;
1295 int status;
1296 unsigned long long id;
1299 if (!video)
1300 goto end;
1302 status = acpi_video_bus_get_POST(video, &id);
1303 if (!ACPI_SUCCESS(status)) {
1304 seq_printf(seq, "<not supported>\n");
1305 goto end;
1307 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
1309 end:
1310 return 0;
1313 static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
1315 struct acpi_video_bus *video = seq->private;
1318 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
1320 return 0;
1323 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
1325 return single_open(file, acpi_video_bus_POST_seq_show,
1326 PDE(inode)->data);
1329 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
1331 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1334 static ssize_t
1335 acpi_video_bus_write_POST(struct file *file,
1336 const char __user * buffer,
1337 size_t count, loff_t * data)
1339 int status;
1340 struct seq_file *m = file->private_data;
1341 struct acpi_video_bus *video = m->private;
1342 char str[12] = { 0 };
1343 unsigned long long opt, options;
1346 if (!video || count + 1 > sizeof str)
1347 return -EINVAL;
1349 status = acpi_video_bus_POST_options(video, &options);
1350 if (!ACPI_SUCCESS(status))
1351 return -EINVAL;
1353 if (copy_from_user(str, buffer, count))
1354 return -EFAULT;
1356 str[count] = 0;
1357 opt = strtoul(str, NULL, 0);
1358 if (opt > 3)
1359 return -EFAULT;
1361 /* just in case an OEM 'forgot' the motherboard... */
1362 options |= 1;
1364 if (options & (1ul << opt)) {
1365 status = acpi_video_bus_set_POST(video, opt);
1366 if (!ACPI_SUCCESS(status))
1367 return -EFAULT;
1371 return count;
1374 static ssize_t
1375 acpi_video_bus_write_DOS(struct file *file,
1376 const char __user * buffer,
1377 size_t count, loff_t * data)
1379 int status;
1380 struct seq_file *m = file->private_data;
1381 struct acpi_video_bus *video = m->private;
1382 char str[12] = { 0 };
1383 unsigned long opt;
1386 if (!video || count + 1 > sizeof str)
1387 return -EINVAL;
1389 if (copy_from_user(str, buffer, count))
1390 return -EFAULT;
1392 str[count] = 0;
1393 opt = strtoul(str, NULL, 0);
1394 if (opt > 7)
1395 return -EFAULT;
1397 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
1399 if (!ACPI_SUCCESS(status))
1400 return -EFAULT;
1402 return count;
1405 static int acpi_video_bus_add_fs(struct acpi_device *device)
1407 struct acpi_video_bus *video = acpi_driver_data(device);
1408 struct proc_dir_entry *device_dir;
1409 struct proc_dir_entry *entry;
1411 device_dir = proc_mkdir(acpi_device_bid(device), acpi_video_dir);
1412 if (!device_dir)
1413 return -ENOMEM;
1415 device_dir->owner = THIS_MODULE;
1417 /* 'info' [R] */
1418 entry = proc_create_data("info", S_IRUGO, device_dir,
1419 &acpi_video_bus_info_fops,
1420 acpi_driver_data(device));
1421 if (!entry)
1422 goto err_remove_dir;
1424 /* 'ROM' [R] */
1425 entry = proc_create_data("ROM", S_IRUGO, device_dir,
1426 &acpi_video_bus_ROM_fops,
1427 acpi_driver_data(device));
1428 if (!entry)
1429 goto err_remove_info;
1431 /* 'POST_info' [R] */
1432 entry = proc_create_data("POST_info", S_IRUGO, device_dir,
1433 &acpi_video_bus_POST_info_fops,
1434 acpi_driver_data(device));
1435 if (!entry)
1436 goto err_remove_rom;
1438 /* 'POST' [R/W] */
1439 entry = proc_create_data("POST", S_IFREG | S_IRUGO | S_IWUSR,
1440 device_dir,
1441 &acpi_video_bus_POST_fops,
1442 acpi_driver_data(device));
1443 if (!entry)
1444 goto err_remove_post_info;
1446 /* 'DOS' [R/W] */
1447 entry = proc_create_data("DOS", S_IFREG | S_IRUGO | S_IWUSR,
1448 device_dir,
1449 &acpi_video_bus_DOS_fops,
1450 acpi_driver_data(device));
1451 if (!entry)
1452 goto err_remove_post;
1454 video->dir = acpi_device_dir(device) = device_dir;
1455 return 0;
1457 err_remove_post:
1458 remove_proc_entry("POST", device_dir);
1459 err_remove_post_info:
1460 remove_proc_entry("POST_info", device_dir);
1461 err_remove_rom:
1462 remove_proc_entry("ROM", device_dir);
1463 err_remove_info:
1464 remove_proc_entry("info", device_dir);
1465 err_remove_dir:
1466 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1467 return -ENOMEM;
1470 static int acpi_video_bus_remove_fs(struct acpi_device *device)
1472 struct proc_dir_entry *device_dir = acpi_device_dir(device);
1474 if (device_dir) {
1475 remove_proc_entry("info", device_dir);
1476 remove_proc_entry("ROM", device_dir);
1477 remove_proc_entry("POST_info", device_dir);
1478 remove_proc_entry("POST", device_dir);
1479 remove_proc_entry("DOS", device_dir);
1480 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1481 acpi_device_dir(device) = NULL;
1484 return 0;
1487 /* --------------------------------------------------------------------------
1488 Driver Interface
1489 -------------------------------------------------------------------------- */
1491 /* device interface */
1492 static struct acpi_video_device_attrib*
1493 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1495 struct acpi_video_enumerated_device *ids;
1496 int i;
1498 for (i = 0; i < video->attached_count; i++) {
1499 ids = &video->attached_array[i];
1500 if ((ids->value.int_val & 0xffff) == device_id)
1501 return &ids->value.attrib;
1504 return NULL;
1507 static int
1508 acpi_video_bus_get_one_device(struct acpi_device *device,
1509 struct acpi_video_bus *video)
1511 unsigned long long device_id;
1512 int status;
1513 struct acpi_video_device *data;
1514 struct acpi_video_device_attrib* attribute;
1516 if (!device || !video)
1517 return -EINVAL;
1519 status =
1520 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1521 if (ACPI_SUCCESS(status)) {
1523 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1524 if (!data)
1525 return -ENOMEM;
1527 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1528 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1529 device->driver_data = data;
1531 data->device_id = device_id;
1532 data->video = video;
1533 data->dev = device;
1535 attribute = acpi_video_get_device_attr(video, device_id);
1537 if((attribute != NULL) && attribute->device_id_scheme) {
1538 switch (attribute->display_type) {
1539 case ACPI_VIDEO_DISPLAY_CRT:
1540 data->flags.crt = 1;
1541 break;
1542 case ACPI_VIDEO_DISPLAY_TV:
1543 data->flags.tvout = 1;
1544 break;
1545 case ACPI_VIDEO_DISPLAY_DVI:
1546 data->flags.dvi = 1;
1547 break;
1548 case ACPI_VIDEO_DISPLAY_LCD:
1549 data->flags.lcd = 1;
1550 break;
1551 default:
1552 data->flags.unknown = 1;
1553 break;
1555 if(attribute->bios_can_detect)
1556 data->flags.bios = 1;
1557 } else
1558 data->flags.unknown = 1;
1560 acpi_video_device_bind(video, data);
1561 acpi_video_device_find_cap(data);
1563 status = acpi_install_notify_handler(device->handle,
1564 ACPI_DEVICE_NOTIFY,
1565 acpi_video_device_notify,
1566 data);
1567 if (ACPI_FAILURE(status)) {
1568 printk(KERN_ERR PREFIX
1569 "Error installing notify handler\n");
1570 if(data->brightness)
1571 kfree(data->brightness->levels);
1572 kfree(data->brightness);
1573 kfree(data);
1574 return -ENODEV;
1577 mutex_lock(&video->device_list_lock);
1578 list_add_tail(&data->entry, &video->video_device_list);
1579 mutex_unlock(&video->device_list_lock);
1581 acpi_video_device_add_fs(device);
1583 return 0;
1586 return -ENOENT;
1590 * Arg:
1591 * video : video bus device
1593 * Return:
1594 * none
1596 * Enumerate the video device list of the video bus,
1597 * bind the ids with the corresponding video devices
1598 * under the video bus.
1601 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1603 struct acpi_video_device *dev;
1605 mutex_lock(&video->device_list_lock);
1607 list_for_each_entry(dev, &video->video_device_list, entry)
1608 acpi_video_device_bind(video, dev);
1610 mutex_unlock(&video->device_list_lock);
1614 * Arg:
1615 * video : video bus device
1616 * device : video output device under the video
1617 * bus
1619 * Return:
1620 * none
1622 * Bind the ids with the corresponding video devices
1623 * under the video bus.
1626 static void
1627 acpi_video_device_bind(struct acpi_video_bus *video,
1628 struct acpi_video_device *device)
1630 struct acpi_video_enumerated_device *ids;
1631 int i;
1633 for (i = 0; i < video->attached_count; i++) {
1634 ids = &video->attached_array[i];
1635 if (device->device_id == (ids->value.int_val & 0xffff)) {
1636 ids->bind_info = device;
1637 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1643 * Arg:
1644 * video : video bus device
1646 * Return:
1647 * < 0 : error
1649 * Call _DOD to enumerate all devices attached to display adapter
1653 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1655 int status;
1656 int count;
1657 int i;
1658 struct acpi_video_enumerated_device *active_list;
1659 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1660 union acpi_object *dod = NULL;
1661 union acpi_object *obj;
1663 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1664 if (!ACPI_SUCCESS(status)) {
1665 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1666 return status;
1669 dod = buffer.pointer;
1670 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1671 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1672 status = -EFAULT;
1673 goto out;
1676 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1677 dod->package.count));
1679 active_list = kcalloc(1 + dod->package.count,
1680 sizeof(struct acpi_video_enumerated_device),
1681 GFP_KERNEL);
1682 if (!active_list) {
1683 status = -ENOMEM;
1684 goto out;
1687 count = 0;
1688 for (i = 0; i < dod->package.count; i++) {
1689 obj = &dod->package.elements[i];
1691 if (obj->type != ACPI_TYPE_INTEGER) {
1692 printk(KERN_ERR PREFIX
1693 "Invalid _DOD data in element %d\n", i);
1694 continue;
1697 active_list[count].value.int_val = obj->integer.value;
1698 active_list[count].bind_info = NULL;
1699 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1700 (int)obj->integer.value));
1701 count++;
1704 kfree(video->attached_array);
1706 video->attached_array = active_list;
1707 video->attached_count = count;
1709 out:
1710 kfree(buffer.pointer);
1711 return status;
1714 static int
1715 acpi_video_get_next_level(struct acpi_video_device *device,
1716 u32 level_current, u32 event)
1718 int min, max, min_above, max_below, i, l, delta = 255;
1719 max = max_below = 0;
1720 min = min_above = 255;
1721 /* Find closest level to level_current */
1722 for (i = 2; i < device->brightness->count; i++) {
1723 l = device->brightness->levels[i];
1724 if (abs(l - level_current) < abs(delta)) {
1725 delta = l - level_current;
1726 if (!delta)
1727 break;
1730 /* Ajust level_current to closest available level */
1731 level_current += delta;
1732 for (i = 2; i < device->brightness->count; i++) {
1733 l = device->brightness->levels[i];
1734 if (l < min)
1735 min = l;
1736 if (l > max)
1737 max = l;
1738 if (l < min_above && l > level_current)
1739 min_above = l;
1740 if (l > max_below && l < level_current)
1741 max_below = l;
1744 switch (event) {
1745 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1746 return (level_current < max) ? min_above : min;
1747 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1748 return (level_current < max) ? min_above : max;
1749 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1750 return (level_current > min) ? max_below : min;
1751 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1752 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1753 return 0;
1754 default:
1755 return level_current;
1759 static void
1760 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1762 unsigned long long level_current, level_next;
1763 if (!device->brightness)
1764 return;
1765 acpi_video_device_lcd_get_level_current(device, &level_current);
1766 level_next = acpi_video_get_next_level(device, level_current, event);
1767 acpi_video_device_lcd_set_level(device, level_next);
1770 static int
1771 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1772 struct acpi_device *device)
1774 int status = 0;
1775 struct acpi_device *dev;
1777 acpi_video_device_enumerate(video);
1779 list_for_each_entry(dev, &device->children, node) {
1781 status = acpi_video_bus_get_one_device(dev, video);
1782 if (ACPI_FAILURE(status)) {
1783 printk(KERN_WARNING PREFIX
1784 "Cant attach device");
1785 continue;
1788 return status;
1791 static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1793 acpi_status status;
1794 struct acpi_video_bus *video;
1797 if (!device || !device->video)
1798 return -ENOENT;
1800 video = device->video;
1802 acpi_video_device_remove_fs(device->dev);
1804 status = acpi_remove_notify_handler(device->dev->handle,
1805 ACPI_DEVICE_NOTIFY,
1806 acpi_video_device_notify);
1807 backlight_device_unregister(device->backlight);
1808 if (device->cdev) {
1809 sysfs_remove_link(&device->dev->dev.kobj,
1810 "thermal_cooling");
1811 sysfs_remove_link(&device->cdev->device.kobj,
1812 "device");
1813 thermal_cooling_device_unregister(device->cdev);
1814 device->cdev = NULL;
1816 video_output_unregister(device->output_dev);
1818 return 0;
1821 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1823 int status;
1824 struct acpi_video_device *dev, *next;
1826 mutex_lock(&video->device_list_lock);
1828 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1830 status = acpi_video_bus_put_one_device(dev);
1831 if (ACPI_FAILURE(status))
1832 printk(KERN_WARNING PREFIX
1833 "hhuuhhuu bug in acpi video driver.\n");
1835 if (dev->brightness) {
1836 kfree(dev->brightness->levels);
1837 kfree(dev->brightness);
1839 list_del(&dev->entry);
1840 kfree(dev);
1843 mutex_unlock(&video->device_list_lock);
1845 return 0;
1848 /* acpi_video interface */
1850 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1852 return acpi_video_bus_DOS(video, 0, 0);
1855 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1857 return acpi_video_bus_DOS(video, 0, 1);
1860 static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
1862 struct acpi_video_bus *video = data;
1863 struct acpi_device *device = NULL;
1864 struct input_dev *input;
1865 int keycode;
1867 if (!video)
1868 return;
1870 device = video->device;
1871 input = video->input;
1873 switch (event) {
1874 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
1875 * most likely via hotkey. */
1876 acpi_bus_generate_proc_event(device, event, 0);
1877 keycode = KEY_SWITCHVIDEOMODE;
1878 break;
1880 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
1881 * connector. */
1882 acpi_video_device_enumerate(video);
1883 acpi_video_device_rebind(video);
1884 acpi_bus_generate_proc_event(device, event, 0);
1885 keycode = KEY_SWITCHVIDEOMODE;
1886 break;
1888 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
1889 acpi_bus_generate_proc_event(device, event, 0);
1890 keycode = KEY_SWITCHVIDEOMODE;
1891 break;
1892 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
1893 acpi_bus_generate_proc_event(device, event, 0);
1894 keycode = KEY_VIDEO_NEXT;
1895 break;
1896 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
1897 acpi_bus_generate_proc_event(device, event, 0);
1898 keycode = KEY_VIDEO_PREV;
1899 break;
1901 default:
1902 keycode = KEY_UNKNOWN;
1903 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1904 "Unsupported event [0x%x]\n", event));
1905 break;
1908 acpi_notifier_call_chain(device, event, 0);
1909 input_report_key(input, keycode, 1);
1910 input_sync(input);
1911 input_report_key(input, keycode, 0);
1912 input_sync(input);
1914 return;
1917 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1919 struct acpi_video_device *video_device = data;
1920 struct acpi_device *device = NULL;
1921 struct acpi_video_bus *bus;
1922 struct input_dev *input;
1923 int keycode;
1925 if (!video_device)
1926 return;
1928 device = video_device->dev;
1929 bus = video_device->video;
1930 input = bus->input;
1932 switch (event) {
1933 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
1934 if (brightness_switch_enabled)
1935 acpi_video_switch_brightness(video_device, event);
1936 acpi_bus_generate_proc_event(device, event, 0);
1937 keycode = KEY_BRIGHTNESS_CYCLE;
1938 break;
1939 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
1940 if (brightness_switch_enabled)
1941 acpi_video_switch_brightness(video_device, event);
1942 acpi_bus_generate_proc_event(device, event, 0);
1943 keycode = KEY_BRIGHTNESSUP;
1944 break;
1945 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
1946 if (brightness_switch_enabled)
1947 acpi_video_switch_brightness(video_device, event);
1948 acpi_bus_generate_proc_event(device, event, 0);
1949 keycode = KEY_BRIGHTNESSDOWN;
1950 break;
1951 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
1952 if (brightness_switch_enabled)
1953 acpi_video_switch_brightness(video_device, event);
1954 acpi_bus_generate_proc_event(device, event, 0);
1955 keycode = KEY_BRIGHTNESS_ZERO;
1956 break;
1957 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
1958 if (brightness_switch_enabled)
1959 acpi_video_switch_brightness(video_device, event);
1960 acpi_bus_generate_proc_event(device, event, 0);
1961 keycode = KEY_DISPLAY_OFF;
1962 break;
1963 default:
1964 keycode = KEY_UNKNOWN;
1965 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1966 "Unsupported event [0x%x]\n", event));
1967 break;
1970 acpi_notifier_call_chain(device, event, 0);
1971 input_report_key(input, keycode, 1);
1972 input_sync(input);
1973 input_report_key(input, keycode, 0);
1974 input_sync(input);
1976 return;
1979 static int instance;
1980 static int acpi_video_resume(struct acpi_device *device)
1982 struct acpi_video_bus *video;
1983 struct acpi_video_device *video_device;
1984 int i;
1986 if (!device || !acpi_driver_data(device))
1987 return -EINVAL;
1989 video = acpi_driver_data(device);
1991 for (i = 0; i < video->attached_count; i++) {
1992 video_device = video->attached_array[i].bind_info;
1993 if (video_device && video_device->backlight)
1994 acpi_video_set_brightness(video_device->backlight);
1996 return AE_OK;
1999 static int acpi_video_bus_add(struct acpi_device *device)
2001 acpi_status status;
2002 struct acpi_video_bus *video;
2003 struct input_dev *input;
2004 int error;
2006 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
2007 if (!video)
2008 return -ENOMEM;
2010 /* a hack to fix the duplicate name "VID" problem on T61 */
2011 if (!strcmp(device->pnp.bus_id, "VID")) {
2012 if (instance)
2013 device->pnp.bus_id[3] = '0' + instance;
2014 instance ++;
2016 /* a hack to fix the duplicate name "VGA" problem on Pa 3553 */
2017 if (!strcmp(device->pnp.bus_id, "VGA")) {
2018 if (instance)
2019 device->pnp.bus_id[3] = '0' + instance;
2020 instance++;
2023 video->device = device;
2024 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
2025 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
2026 device->driver_data = video;
2028 acpi_video_bus_find_cap(video);
2029 error = acpi_video_bus_check(video);
2030 if (error)
2031 goto err_free_video;
2033 error = acpi_video_bus_add_fs(device);
2034 if (error)
2035 goto err_free_video;
2037 mutex_init(&video->device_list_lock);
2038 INIT_LIST_HEAD(&video->video_device_list);
2040 acpi_video_bus_get_devices(video, device);
2041 acpi_video_bus_start_devices(video);
2043 status = acpi_install_notify_handler(device->handle,
2044 ACPI_DEVICE_NOTIFY,
2045 acpi_video_bus_notify, video);
2046 if (ACPI_FAILURE(status)) {
2047 printk(KERN_ERR PREFIX
2048 "Error installing notify handler\n");
2049 error = -ENODEV;
2050 goto err_stop_video;
2053 video->input = input = input_allocate_device();
2054 if (!input) {
2055 error = -ENOMEM;
2056 goto err_uninstall_notify;
2059 snprintf(video->phys, sizeof(video->phys),
2060 "%s/video/input0", acpi_device_hid(video->device));
2062 input->name = acpi_device_name(video->device);
2063 input->phys = video->phys;
2064 input->id.bustype = BUS_HOST;
2065 input->id.product = 0x06;
2066 input->dev.parent = &device->dev;
2067 input->evbit[0] = BIT(EV_KEY);
2068 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
2069 set_bit(KEY_VIDEO_NEXT, input->keybit);
2070 set_bit(KEY_VIDEO_PREV, input->keybit);
2071 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
2072 set_bit(KEY_BRIGHTNESSUP, input->keybit);
2073 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
2074 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
2075 set_bit(KEY_DISPLAY_OFF, input->keybit);
2076 set_bit(KEY_UNKNOWN, input->keybit);
2078 error = input_register_device(input);
2079 if (error)
2080 goto err_free_input_dev;
2082 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
2083 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
2084 video->flags.multihead ? "yes" : "no",
2085 video->flags.rom ? "yes" : "no",
2086 video->flags.post ? "yes" : "no");
2088 return 0;
2090 err_free_input_dev:
2091 input_free_device(input);
2092 err_uninstall_notify:
2093 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
2094 acpi_video_bus_notify);
2095 err_stop_video:
2096 acpi_video_bus_stop_devices(video);
2097 acpi_video_bus_put_devices(video);
2098 kfree(video->attached_array);
2099 acpi_video_bus_remove_fs(device);
2100 err_free_video:
2101 kfree(video);
2102 device->driver_data = NULL;
2104 return error;
2107 static int acpi_video_bus_remove(struct acpi_device *device, int type)
2109 acpi_status status = 0;
2110 struct acpi_video_bus *video = NULL;
2113 if (!device || !acpi_driver_data(device))
2114 return -EINVAL;
2116 video = acpi_driver_data(device);
2118 acpi_video_bus_stop_devices(video);
2120 status = acpi_remove_notify_handler(video->device->handle,
2121 ACPI_DEVICE_NOTIFY,
2122 acpi_video_bus_notify);
2124 acpi_video_bus_put_devices(video);
2125 acpi_video_bus_remove_fs(device);
2127 input_unregister_device(video->input);
2128 kfree(video->attached_array);
2129 kfree(video);
2131 return 0;
2134 static int __init acpi_video_init(void)
2136 int result = 0;
2138 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2139 if (!acpi_video_dir)
2140 return -ENODEV;
2141 acpi_video_dir->owner = THIS_MODULE;
2143 result = acpi_bus_register_driver(&acpi_video_bus);
2144 if (result < 0) {
2145 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2146 return -ENODEV;
2149 return 0;
2152 static void __exit acpi_video_exit(void)
2155 acpi_bus_unregister_driver(&acpi_video_bus);
2157 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2159 return;
2162 module_init(acpi_video_init);
2163 module_exit(acpi_video_exit);