ACPI: video: Rationalise ACPI backlight implementation
[linux-2.6/libata-dev.git] / drivers / acpi / video.c
blob59639c9c66664f5a3c0444f32484209fcca1ea3c
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/video_output.h>
38 #include <asm/uaccess.h>
40 #include <acpi/acpi_bus.h>
41 #include <acpi/acpi_drivers.h>
43 #define ACPI_VIDEO_COMPONENT 0x08000000
44 #define ACPI_VIDEO_CLASS "video"
45 #define ACPI_VIDEO_BUS_NAME "Video Bus"
46 #define ACPI_VIDEO_DEVICE_NAME "Video Device"
47 #define ACPI_VIDEO_NOTIFY_SWITCH 0x80
48 #define ACPI_VIDEO_NOTIFY_PROBE 0x81
49 #define ACPI_VIDEO_NOTIFY_CYCLE 0x82
50 #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT 0x83
51 #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT 0x84
53 #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS 0x85
54 #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS 0x86
55 #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS 0x87
56 #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS 0x88
57 #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF 0x89
59 #define ACPI_VIDEO_HEAD_INVALID (~0u - 1)
60 #define ACPI_VIDEO_HEAD_END (~0u)
61 #define MAX_NAME_LEN 20
63 #define ACPI_VIDEO_DISPLAY_CRT 1
64 #define ACPI_VIDEO_DISPLAY_TV 2
65 #define ACPI_VIDEO_DISPLAY_DVI 3
66 #define ACPI_VIDEO_DISPLAY_LCD 4
68 #define _COMPONENT ACPI_VIDEO_COMPONENT
69 ACPI_MODULE_NAME("video");
71 MODULE_AUTHOR("Bruno Ducrot");
72 MODULE_DESCRIPTION("ACPI Video Driver");
73 MODULE_LICENSE("GPL");
75 static int acpi_video_bus_add(struct acpi_device *device);
76 static int acpi_video_bus_remove(struct acpi_device *device, int type);
78 static const struct acpi_device_id video_device_ids[] = {
79 {ACPI_VIDEO_HID, 0},
80 {"", 0},
82 MODULE_DEVICE_TABLE(acpi, video_device_ids);
84 static struct acpi_driver acpi_video_bus = {
85 .name = "video",
86 .class = ACPI_VIDEO_CLASS,
87 .ids = video_device_ids,
88 .ops = {
89 .add = acpi_video_bus_add,
90 .remove = acpi_video_bus_remove,
94 struct acpi_video_bus_flags {
95 u8 multihead:1; /* can switch video heads */
96 u8 rom:1; /* can retrieve a video rom */
97 u8 post:1; /* can configure the head to */
98 u8 reserved:5;
101 struct acpi_video_bus_cap {
102 u8 _DOS:1; /*Enable/Disable output switching */
103 u8 _DOD:1; /*Enumerate all devices attached to display adapter */
104 u8 _ROM:1; /*Get ROM Data */
105 u8 _GPD:1; /*Get POST Device */
106 u8 _SPD:1; /*Set POST Device */
107 u8 _VPO:1; /*Video POST Options */
108 u8 reserved:2;
111 struct acpi_video_device_attrib {
112 u32 display_index:4; /* A zero-based instance of the Display */
113 u32 display_port_attachment:4; /*This field differentiates the display type */
114 u32 display_type:4; /*Describe the specific type in use */
115 u32 vendor_specific:4; /*Chipset Vendor Specific */
116 u32 bios_can_detect:1; /*BIOS can detect the device */
117 u32 depend_on_vga:1; /*Non-VGA output device whose power is related to
118 the VGA device. */
119 u32 pipe_id:3; /*For VGA multiple-head devices. */
120 u32 reserved:10; /*Must be 0 */
121 u32 device_id_scheme:1; /*Device ID Scheme */
124 struct acpi_video_enumerated_device {
125 union {
126 u32 int_val;
127 struct acpi_video_device_attrib attrib;
128 } value;
129 struct acpi_video_device *bind_info;
132 struct acpi_video_bus {
133 struct acpi_device *device;
134 u8 dos_setting;
135 struct acpi_video_enumerated_device *attached_array;
136 u8 attached_count;
137 struct acpi_video_bus_cap cap;
138 struct acpi_video_bus_flags flags;
139 struct list_head video_device_list;
140 struct mutex device_list_lock; /* protects video_device_list */
141 struct proc_dir_entry *dir;
142 struct input_dev *input;
143 char phys[32]; /* for input device */
146 struct acpi_video_device_flags {
147 u8 crt:1;
148 u8 lcd:1;
149 u8 tvout:1;
150 u8 dvi:1;
151 u8 bios:1;
152 u8 unknown:1;
153 u8 reserved:2;
156 struct acpi_video_device_cap {
157 u8 _ADR:1; /*Return the unique ID */
158 u8 _BCL:1; /*Query list of brightness control levels supported */
159 u8 _BCM:1; /*Set the brightness level */
160 u8 _BQC:1; /* Get current brightness level */
161 u8 _DDC:1; /*Return the EDID for this device */
162 u8 _DCS:1; /*Return status of output device */
163 u8 _DGS:1; /*Query graphics state */
164 u8 _DSS:1; /*Device state set */
167 struct acpi_video_device_brightness {
168 int curr;
169 int count;
170 int *levels;
173 struct acpi_video_device {
174 unsigned long device_id;
175 struct acpi_video_device_flags flags;
176 struct acpi_video_device_cap cap;
177 struct list_head entry;
178 struct acpi_video_bus *video;
179 struct acpi_device *dev;
180 struct acpi_video_device_brightness *brightness;
181 struct backlight_device *backlight;
182 struct output_device *output_dev;
185 /* bus */
186 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
187 static struct file_operations acpi_video_bus_info_fops = {
188 .open = acpi_video_bus_info_open_fs,
189 .read = seq_read,
190 .llseek = seq_lseek,
191 .release = single_release,
194 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
195 static struct file_operations acpi_video_bus_ROM_fops = {
196 .open = acpi_video_bus_ROM_open_fs,
197 .read = seq_read,
198 .llseek = seq_lseek,
199 .release = single_release,
202 static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
203 struct file *file);
204 static struct file_operations acpi_video_bus_POST_info_fops = {
205 .open = acpi_video_bus_POST_info_open_fs,
206 .read = seq_read,
207 .llseek = seq_lseek,
208 .release = single_release,
211 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
212 static struct file_operations acpi_video_bus_POST_fops = {
213 .open = acpi_video_bus_POST_open_fs,
214 .read = seq_read,
215 .llseek = seq_lseek,
216 .release = single_release,
219 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
220 static struct file_operations acpi_video_bus_DOS_fops = {
221 .open = acpi_video_bus_DOS_open_fs,
222 .read = seq_read,
223 .llseek = seq_lseek,
224 .release = single_release,
227 /* device */
228 static int acpi_video_device_info_open_fs(struct inode *inode,
229 struct file *file);
230 static struct file_operations acpi_video_device_info_fops = {
231 .open = acpi_video_device_info_open_fs,
232 .read = seq_read,
233 .llseek = seq_lseek,
234 .release = single_release,
237 static int acpi_video_device_state_open_fs(struct inode *inode,
238 struct file *file);
239 static struct file_operations acpi_video_device_state_fops = {
240 .open = acpi_video_device_state_open_fs,
241 .read = seq_read,
242 .llseek = seq_lseek,
243 .release = single_release,
246 static int acpi_video_device_brightness_open_fs(struct inode *inode,
247 struct file *file);
248 static struct file_operations acpi_video_device_brightness_fops = {
249 .open = acpi_video_device_brightness_open_fs,
250 .read = seq_read,
251 .llseek = seq_lseek,
252 .release = single_release,
255 static int acpi_video_device_EDID_open_fs(struct inode *inode,
256 struct file *file);
257 static struct file_operations acpi_video_device_EDID_fops = {
258 .open = acpi_video_device_EDID_open_fs,
259 .read = seq_read,
260 .llseek = seq_lseek,
261 .release = single_release,
264 static char device_decode[][30] = {
265 "motherboard VGA device",
266 "PCI VGA device",
267 "AGP VGA device",
268 "UNKNOWN",
271 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
272 static void acpi_video_device_rebind(struct acpi_video_bus *video);
273 static void acpi_video_device_bind(struct acpi_video_bus *video,
274 struct acpi_video_device *device);
275 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
276 static int acpi_video_switch_output(struct acpi_video_bus *video, int event);
277 static int acpi_video_device_lcd_set_level(struct acpi_video_device *device,
278 int level);
279 static int acpi_video_device_lcd_get_level_current(
280 struct acpi_video_device *device,
281 unsigned long *level);
282 static int acpi_video_get_next_level(struct acpi_video_device *device,
283 u32 level_current, u32 event);
284 static void acpi_video_switch_brightness(struct acpi_video_device *device,
285 int event);
286 static int acpi_video_device_get_state(struct acpi_video_device *device,
287 unsigned long *state);
288 static int acpi_video_output_get(struct output_device *od);
289 static int acpi_video_device_set_state(struct acpi_video_device *device, int state);
291 /*backlight device sysfs support*/
292 static int acpi_video_get_brightness(struct backlight_device *bd)
294 unsigned long cur_level;
295 int i;
296 struct acpi_video_device *vd =
297 (struct acpi_video_device *)bl_get_data(bd);
298 acpi_video_device_lcd_get_level_current(vd, &cur_level);
299 for (i = 2; i < vd->brightness->count; i++) {
300 if (vd->brightness->levels[i] == cur_level)
301 /* The first two entries are special - see page 575
302 of the ACPI spec 3.0 */
303 return i-2;
305 return 0;
308 static int acpi_video_set_brightness(struct backlight_device *bd)
310 int request_level = bd->props.brightness+2;
311 struct acpi_video_device *vd =
312 (struct acpi_video_device *)bl_get_data(bd);
313 acpi_video_device_lcd_set_level(vd,
314 vd->brightness->levels[request_level]);
315 return 0;
318 static struct backlight_ops acpi_backlight_ops = {
319 .get_brightness = acpi_video_get_brightness,
320 .update_status = acpi_video_set_brightness,
323 /*video output device sysfs support*/
324 static int acpi_video_output_get(struct output_device *od)
326 unsigned long state;
327 struct acpi_video_device *vd =
328 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
329 acpi_video_device_get_state(vd, &state);
330 return (int)state;
333 static int acpi_video_output_set(struct output_device *od)
335 unsigned long state = od->request_state;
336 struct acpi_video_device *vd=
337 (struct acpi_video_device *)dev_get_drvdata(&od->dev);
338 return acpi_video_device_set_state(vd, state);
341 static struct output_properties acpi_output_properties = {
342 .set_state = acpi_video_output_set,
343 .get_status = acpi_video_output_get,
345 /* --------------------------------------------------------------------------
346 Video Management
347 -------------------------------------------------------------------------- */
349 /* device */
351 static int
352 acpi_video_device_query(struct acpi_video_device *device, unsigned long *state)
354 int status;
356 status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
358 return status;
361 static int
362 acpi_video_device_get_state(struct acpi_video_device *device,
363 unsigned long *state)
365 int status;
367 status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
369 return status;
372 static int
373 acpi_video_device_set_state(struct acpi_video_device *device, int state)
375 int status;
376 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
377 struct acpi_object_list args = { 1, &arg0 };
378 unsigned long ret;
381 arg0.integer.value = state;
382 status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
384 return status;
387 static int
388 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
389 union acpi_object **levels)
391 int status;
392 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
393 union acpi_object *obj;
396 *levels = NULL;
398 status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
399 if (!ACPI_SUCCESS(status))
400 return status;
401 obj = (union acpi_object *)buffer.pointer;
402 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
403 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
404 status = -EFAULT;
405 goto err;
408 *levels = obj;
410 return 0;
412 err:
413 kfree(buffer.pointer);
415 return status;
418 static int
419 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
421 int status = AE_OK;
422 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
423 struct acpi_object_list args = { 1, &arg0 };
426 arg0.integer.value = level;
428 if (device->cap._BCM)
429 status = acpi_evaluate_object(device->dev->handle, "_BCM",
430 &args, NULL);
431 device->brightness->curr = level;
432 return status;
435 static int
436 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
437 unsigned long *level)
439 if (device->cap._BQC)
440 return acpi_evaluate_integer(device->dev->handle, "_BQC", NULL,
441 level);
442 *level = device->brightness->curr;
443 return AE_OK;
446 static int
447 acpi_video_device_EDID(struct acpi_video_device *device,
448 union acpi_object **edid, ssize_t length)
450 int status;
451 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
452 union acpi_object *obj;
453 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
454 struct acpi_object_list args = { 1, &arg0 };
457 *edid = NULL;
459 if (!device)
460 return -ENODEV;
461 if (length == 128)
462 arg0.integer.value = 1;
463 else if (length == 256)
464 arg0.integer.value = 2;
465 else
466 return -EINVAL;
468 status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
469 if (ACPI_FAILURE(status))
470 return -ENODEV;
472 obj = buffer.pointer;
474 if (obj && obj->type == ACPI_TYPE_BUFFER)
475 *edid = obj;
476 else {
477 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
478 status = -EFAULT;
479 kfree(obj);
482 return status;
485 /* bus */
487 static int
488 acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
490 int status;
491 unsigned long tmp;
492 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
493 struct acpi_object_list args = { 1, &arg0 };
496 arg0.integer.value = option;
498 status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
499 if (ACPI_SUCCESS(status))
500 status = tmp ? (-EINVAL) : (AE_OK);
502 return status;
505 static int
506 acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long *id)
508 int status;
510 status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
512 return status;
515 static int
516 acpi_video_bus_POST_options(struct acpi_video_bus *video,
517 unsigned long *options)
519 int status;
521 status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
522 *options &= 3;
524 return status;
528 * Arg:
529 * video : video bus device pointer
530 * bios_flag :
531 * 0. The system BIOS should NOT automatically switch(toggle)
532 * the active display output.
533 * 1. The system BIOS should automatically switch (toggle) the
534 * active display output. No switch event.
535 * 2. The _DGS value should be locked.
536 * 3. The system BIOS should not automatically switch (toggle) the
537 * active display output, but instead generate the display switch
538 * event notify code.
539 * lcd_flag :
540 * 0. The system BIOS should automatically control the brightness level
541 * of the LCD when the power changes from AC to DC
542 * 1. The system BIOS should NOT automatically control the brightness
543 * level of the LCD when the power changes from AC to DC.
544 * Return Value:
545 * -1 wrong arg.
548 static int
549 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
551 acpi_integer status = 0;
552 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
553 struct acpi_object_list args = { 1, &arg0 };
556 if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
557 status = -1;
558 goto Failed;
560 arg0.integer.value = (lcd_flag << 2) | bios_flag;
561 video->dos_setting = arg0.integer.value;
562 acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
564 Failed:
565 return status;
569 * Arg:
570 * device : video output device (LCD, CRT, ..)
572 * Return Value:
573 * None
575 * Find out all required AML methods defined under the output
576 * device.
579 static void acpi_video_device_find_cap(struct acpi_video_device *device)
581 acpi_handle h_dummy1;
582 int i;
583 u32 max_level = 0;
584 union acpi_object *obj = NULL;
585 struct acpi_video_device_brightness *br = NULL;
588 memset(&device->cap, 0, sizeof(device->cap));
590 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
591 device->cap._ADR = 1;
593 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
594 device->cap._BCL = 1;
596 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
597 device->cap._BCM = 1;
599 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle,"_BQC",&h_dummy1)))
600 device->cap._BQC = 1;
601 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
602 device->cap._DDC = 1;
604 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
605 device->cap._DCS = 1;
607 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
608 device->cap._DGS = 1;
610 if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
611 device->cap._DSS = 1;
614 if (ACPI_SUCCESS(acpi_video_device_lcd_query_levels(device, &obj))) {
616 if (obj->package.count >= 2) {
617 int count = 0;
618 union acpi_object *o;
620 br = kzalloc(sizeof(*br), GFP_KERNEL);
621 if (!br) {
622 printk(KERN_ERR "can't allocate memory\n");
623 } else {
624 br->levels = kmalloc(obj->package.count *
625 sizeof *(br->levels), GFP_KERNEL);
626 if (!br->levels)
627 goto out;
629 for (i = 0; i < obj->package.count; i++) {
630 o = (union acpi_object *)&obj->package.
631 elements[i];
632 if (o->type != ACPI_TYPE_INTEGER) {
633 printk(KERN_ERR PREFIX "Invalid data\n");
634 continue;
636 br->levels[count] = (u32) o->integer.value;
638 if (br->levels[count] > max_level)
639 max_level = br->levels[count];
640 count++;
642 out:
643 if (count < 2) {
644 kfree(br->levels);
645 kfree(br);
646 } else {
647 br->count = count;
648 device->brightness = br;
649 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
650 "found %d brightness levels\n",
651 count));
656 } else {
657 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Could not query available LCD brightness level\n"));
660 kfree(obj);
662 if (device->cap._BCL && device->cap._BCM && device->cap._BQC && max_level > 0){
663 static int count = 0;
664 char *name;
665 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
666 if (!name)
667 return;
669 sprintf(name, "acpi_video%d", count++);
670 device->backlight = backlight_device_register(name,
671 NULL, device, &acpi_backlight_ops);
672 device->backlight->props.max_brightness = device->brightness->count-3;
673 device->backlight->props.brightness = acpi_video_get_brightness(device->backlight);
674 backlight_update_status(device->backlight);
676 kfree(name);
678 if (device->cap._DCS && device->cap._DSS){
679 static int count = 0;
680 char *name;
681 name = kzalloc(MAX_NAME_LEN, GFP_KERNEL);
682 if (!name)
683 return;
684 sprintf(name, "acpi_video%d", count++);
685 device->output_dev = video_output_register(name,
686 NULL, device, &acpi_output_properties);
687 kfree(name);
689 return;
693 * Arg:
694 * device : video output device (VGA)
696 * Return Value:
697 * None
699 * Find out all required AML methods defined under the video bus device.
702 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
704 acpi_handle h_dummy1;
706 memset(&video->cap, 0, sizeof(video->cap));
707 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
708 video->cap._DOS = 1;
710 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
711 video->cap._DOD = 1;
713 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
714 video->cap._ROM = 1;
716 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
717 video->cap._GPD = 1;
719 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
720 video->cap._SPD = 1;
722 if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
723 video->cap._VPO = 1;
728 * Check whether the video bus device has required AML method to
729 * support the desired features
732 static int acpi_video_bus_check(struct acpi_video_bus *video)
734 acpi_status status = -ENOENT;
737 if (!video)
738 return -EINVAL;
740 /* Since there is no HID, CID and so on for VGA driver, we have
741 * to check well known required nodes.
744 /* Does this device support video switching? */
745 if (video->cap._DOS) {
746 video->flags.multihead = 1;
747 status = 0;
750 /* Does this device support retrieving a video ROM? */
751 if (video->cap._ROM) {
752 video->flags.rom = 1;
753 status = 0;
756 /* Does this device support configuring which video device to POST? */
757 if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
758 video->flags.post = 1;
759 status = 0;
762 return status;
765 /* --------------------------------------------------------------------------
766 FS Interface (/proc)
767 -------------------------------------------------------------------------- */
769 static struct proc_dir_entry *acpi_video_dir;
771 /* video devices */
773 static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
775 struct acpi_video_device *dev = seq->private;
778 if (!dev)
779 goto end;
781 seq_printf(seq, "device_id: 0x%04x\n", (u32) dev->device_id);
782 seq_printf(seq, "type: ");
783 if (dev->flags.crt)
784 seq_printf(seq, "CRT\n");
785 else if (dev->flags.lcd)
786 seq_printf(seq, "LCD\n");
787 else if (dev->flags.tvout)
788 seq_printf(seq, "TVOUT\n");
789 else if (dev->flags.dvi)
790 seq_printf(seq, "DVI\n");
791 else
792 seq_printf(seq, "UNKNOWN\n");
794 seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
796 end:
797 return 0;
800 static int
801 acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
803 return single_open(file, acpi_video_device_info_seq_show,
804 PDE(inode)->data);
807 static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
809 int status;
810 struct acpi_video_device *dev = seq->private;
811 unsigned long state;
814 if (!dev)
815 goto end;
817 status = acpi_video_device_get_state(dev, &state);
818 seq_printf(seq, "state: ");
819 if (ACPI_SUCCESS(status))
820 seq_printf(seq, "0x%02lx\n", state);
821 else
822 seq_printf(seq, "<not supported>\n");
824 status = acpi_video_device_query(dev, &state);
825 seq_printf(seq, "query: ");
826 if (ACPI_SUCCESS(status))
827 seq_printf(seq, "0x%02lx\n", state);
828 else
829 seq_printf(seq, "<not supported>\n");
831 end:
832 return 0;
835 static int
836 acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
838 return single_open(file, acpi_video_device_state_seq_show,
839 PDE(inode)->data);
842 static ssize_t
843 acpi_video_device_write_state(struct file *file,
844 const char __user * buffer,
845 size_t count, loff_t * data)
847 int status;
848 struct seq_file *m = file->private_data;
849 struct acpi_video_device *dev = m->private;
850 char str[12] = { 0 };
851 u32 state = 0;
854 if (!dev || count + 1 > sizeof str)
855 return -EINVAL;
857 if (copy_from_user(str, buffer, count))
858 return -EFAULT;
860 str[count] = 0;
861 state = simple_strtoul(str, NULL, 0);
862 state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
864 status = acpi_video_device_set_state(dev, state);
866 if (status)
867 return -EFAULT;
869 return count;
872 static int
873 acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
875 struct acpi_video_device *dev = seq->private;
876 int i;
879 if (!dev || !dev->brightness) {
880 seq_printf(seq, "<not supported>\n");
881 return 0;
884 seq_printf(seq, "levels: ");
885 for (i = 0; i < dev->brightness->count; i++)
886 seq_printf(seq, " %d", dev->brightness->levels[i]);
887 seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
889 return 0;
892 static int
893 acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
895 return single_open(file, acpi_video_device_brightness_seq_show,
896 PDE(inode)->data);
899 static ssize_t
900 acpi_video_device_write_brightness(struct file *file,
901 const char __user * buffer,
902 size_t count, loff_t * data)
904 struct seq_file *m = file->private_data;
905 struct acpi_video_device *dev = m->private;
906 char str[5] = { 0 };
907 unsigned int level = 0;
908 int i;
911 if (!dev || !dev->brightness || count + 1 > sizeof str)
912 return -EINVAL;
914 if (copy_from_user(str, buffer, count))
915 return -EFAULT;
917 str[count] = 0;
918 level = simple_strtoul(str, NULL, 0);
920 if (level > 100)
921 return -EFAULT;
923 /* validate through the list of available levels */
924 for (i = 0; i < dev->brightness->count; i++)
925 if (level == dev->brightness->levels[i]) {
926 if (ACPI_SUCCESS
927 (acpi_video_device_lcd_set_level(dev, level)))
928 dev->brightness->curr = level;
929 break;
932 return count;
935 static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
937 struct acpi_video_device *dev = seq->private;
938 int status;
939 int i;
940 union acpi_object *edid = NULL;
943 if (!dev)
944 goto out;
946 status = acpi_video_device_EDID(dev, &edid, 128);
947 if (ACPI_FAILURE(status)) {
948 status = acpi_video_device_EDID(dev, &edid, 256);
951 if (ACPI_FAILURE(status)) {
952 goto out;
955 if (edid && edid->type == ACPI_TYPE_BUFFER) {
956 for (i = 0; i < edid->buffer.length; i++)
957 seq_putc(seq, edid->buffer.pointer[i]);
960 out:
961 if (!edid)
962 seq_printf(seq, "<not supported>\n");
963 else
964 kfree(edid);
966 return 0;
969 static int
970 acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
972 return single_open(file, acpi_video_device_EDID_seq_show,
973 PDE(inode)->data);
976 static int acpi_video_device_add_fs(struct acpi_device *device)
978 struct proc_dir_entry *entry = NULL;
979 struct acpi_video_device *vid_dev;
982 if (!device)
983 return -ENODEV;
985 vid_dev = acpi_driver_data(device);
986 if (!vid_dev)
987 return -ENODEV;
989 if (!acpi_device_dir(device)) {
990 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
991 vid_dev->video->dir);
992 if (!acpi_device_dir(device))
993 return -ENODEV;
994 acpi_device_dir(device)->owner = THIS_MODULE;
997 /* 'info' [R] */
998 entry = create_proc_entry("info", S_IRUGO, acpi_device_dir(device));
999 if (!entry)
1000 return -ENODEV;
1001 else {
1002 entry->proc_fops = &acpi_video_device_info_fops;
1003 entry->data = acpi_driver_data(device);
1004 entry->owner = THIS_MODULE;
1007 /* 'state' [R/W] */
1008 entry =
1009 create_proc_entry("state", S_IFREG | S_IRUGO | S_IWUSR,
1010 acpi_device_dir(device));
1011 if (!entry)
1012 return -ENODEV;
1013 else {
1014 acpi_video_device_state_fops.write = acpi_video_device_write_state;
1015 entry->proc_fops = &acpi_video_device_state_fops;
1016 entry->data = acpi_driver_data(device);
1017 entry->owner = THIS_MODULE;
1020 /* 'brightness' [R/W] */
1021 entry =
1022 create_proc_entry("brightness", S_IFREG | S_IRUGO | S_IWUSR,
1023 acpi_device_dir(device));
1024 if (!entry)
1025 return -ENODEV;
1026 else {
1027 acpi_video_device_brightness_fops.write = acpi_video_device_write_brightness;
1028 entry->proc_fops = &acpi_video_device_brightness_fops;
1029 entry->data = acpi_driver_data(device);
1030 entry->owner = THIS_MODULE;
1033 /* 'EDID' [R] */
1034 entry = create_proc_entry("EDID", S_IRUGO, acpi_device_dir(device));
1035 if (!entry)
1036 return -ENODEV;
1037 else {
1038 entry->proc_fops = &acpi_video_device_EDID_fops;
1039 entry->data = acpi_driver_data(device);
1040 entry->owner = THIS_MODULE;
1043 return 0;
1046 static int acpi_video_device_remove_fs(struct acpi_device *device)
1048 struct acpi_video_device *vid_dev;
1050 vid_dev = acpi_driver_data(device);
1051 if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
1052 return -ENODEV;
1054 if (acpi_device_dir(device)) {
1055 remove_proc_entry("info", acpi_device_dir(device));
1056 remove_proc_entry("state", acpi_device_dir(device));
1057 remove_proc_entry("brightness", acpi_device_dir(device));
1058 remove_proc_entry("EDID", acpi_device_dir(device));
1059 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
1060 acpi_device_dir(device) = NULL;
1063 return 0;
1066 /* video bus */
1067 static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
1069 struct acpi_video_bus *video = seq->private;
1072 if (!video)
1073 goto end;
1075 seq_printf(seq, "Switching heads: %s\n",
1076 video->flags.multihead ? "yes" : "no");
1077 seq_printf(seq, "Video ROM: %s\n",
1078 video->flags.rom ? "yes" : "no");
1079 seq_printf(seq, "Device to be POSTed on boot: %s\n",
1080 video->flags.post ? "yes" : "no");
1082 end:
1083 return 0;
1086 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
1088 return single_open(file, acpi_video_bus_info_seq_show,
1089 PDE(inode)->data);
1092 static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
1094 struct acpi_video_bus *video = seq->private;
1097 if (!video)
1098 goto end;
1100 printk(KERN_INFO PREFIX "Please implement %s\n", __FUNCTION__);
1101 seq_printf(seq, "<TODO>\n");
1103 end:
1104 return 0;
1107 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
1109 return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
1112 static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
1114 struct acpi_video_bus *video = seq->private;
1115 unsigned long options;
1116 int status;
1119 if (!video)
1120 goto end;
1122 status = acpi_video_bus_POST_options(video, &options);
1123 if (ACPI_SUCCESS(status)) {
1124 if (!(options & 1)) {
1125 printk(KERN_WARNING PREFIX
1126 "The motherboard VGA device is not listed as a possible POST device.\n");
1127 printk(KERN_WARNING PREFIX
1128 "This indicates a BIOS bug. Please contact the manufacturer.\n");
1130 printk("%lx\n", options);
1131 seq_printf(seq, "can POST: <integrated video>");
1132 if (options & 2)
1133 seq_printf(seq, " <PCI video>");
1134 if (options & 4)
1135 seq_printf(seq, " <AGP video>");
1136 seq_putc(seq, '\n');
1137 } else
1138 seq_printf(seq, "<not supported>\n");
1139 end:
1140 return 0;
1143 static int
1144 acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
1146 return single_open(file, acpi_video_bus_POST_info_seq_show,
1147 PDE(inode)->data);
1150 static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
1152 struct acpi_video_bus *video = seq->private;
1153 int status;
1154 unsigned long id;
1157 if (!video)
1158 goto end;
1160 status = acpi_video_bus_get_POST(video, &id);
1161 if (!ACPI_SUCCESS(status)) {
1162 seq_printf(seq, "<not supported>\n");
1163 goto end;
1165 seq_printf(seq, "device POSTed is <%s>\n", device_decode[id & 3]);
1167 end:
1168 return 0;
1171 static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
1173 struct acpi_video_bus *video = seq->private;
1176 seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
1178 return 0;
1181 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
1183 return single_open(file, acpi_video_bus_POST_seq_show,
1184 PDE(inode)->data);
1187 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
1189 return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1192 static ssize_t
1193 acpi_video_bus_write_POST(struct file *file,
1194 const char __user * buffer,
1195 size_t count, loff_t * data)
1197 int status;
1198 struct seq_file *m = file->private_data;
1199 struct acpi_video_bus *video = m->private;
1200 char str[12] = { 0 };
1201 unsigned long opt, options;
1204 if (!video || count + 1 > sizeof str)
1205 return -EINVAL;
1207 status = acpi_video_bus_POST_options(video, &options);
1208 if (!ACPI_SUCCESS(status))
1209 return -EINVAL;
1211 if (copy_from_user(str, buffer, count))
1212 return -EFAULT;
1214 str[count] = 0;
1215 opt = strtoul(str, NULL, 0);
1216 if (opt > 3)
1217 return -EFAULT;
1219 /* just in case an OEM 'forgot' the motherboard... */
1220 options |= 1;
1222 if (options & (1ul << opt)) {
1223 status = acpi_video_bus_set_POST(video, opt);
1224 if (!ACPI_SUCCESS(status))
1225 return -EFAULT;
1229 return count;
1232 static ssize_t
1233 acpi_video_bus_write_DOS(struct file *file,
1234 const char __user * buffer,
1235 size_t count, loff_t * data)
1237 int status;
1238 struct seq_file *m = file->private_data;
1239 struct acpi_video_bus *video = m->private;
1240 char str[12] = { 0 };
1241 unsigned long opt;
1244 if (!video || count + 1 > sizeof str)
1245 return -EINVAL;
1247 if (copy_from_user(str, buffer, count))
1248 return -EFAULT;
1250 str[count] = 0;
1251 opt = strtoul(str, NULL, 0);
1252 if (opt > 7)
1253 return -EFAULT;
1255 status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
1257 if (!ACPI_SUCCESS(status))
1258 return -EFAULT;
1260 return count;
1263 static int acpi_video_bus_add_fs(struct acpi_device *device)
1265 struct proc_dir_entry *entry = NULL;
1266 struct acpi_video_bus *video;
1269 video = acpi_driver_data(device);
1271 if (!acpi_device_dir(device)) {
1272 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1273 acpi_video_dir);
1274 if (!acpi_device_dir(device))
1275 return -ENODEV;
1276 video->dir = acpi_device_dir(device);
1277 acpi_device_dir(device)->owner = THIS_MODULE;
1280 /* 'info' [R] */
1281 entry = create_proc_entry("info", S_IRUGO, acpi_device_dir(device));
1282 if (!entry)
1283 return -ENODEV;
1284 else {
1285 entry->proc_fops = &acpi_video_bus_info_fops;
1286 entry->data = acpi_driver_data(device);
1287 entry->owner = THIS_MODULE;
1290 /* 'ROM' [R] */
1291 entry = create_proc_entry("ROM", S_IRUGO, acpi_device_dir(device));
1292 if (!entry)
1293 return -ENODEV;
1294 else {
1295 entry->proc_fops = &acpi_video_bus_ROM_fops;
1296 entry->data = acpi_driver_data(device);
1297 entry->owner = THIS_MODULE;
1300 /* 'POST_info' [R] */
1301 entry =
1302 create_proc_entry("POST_info", S_IRUGO, acpi_device_dir(device));
1303 if (!entry)
1304 return -ENODEV;
1305 else {
1306 entry->proc_fops = &acpi_video_bus_POST_info_fops;
1307 entry->data = acpi_driver_data(device);
1308 entry->owner = THIS_MODULE;
1311 /* 'POST' [R/W] */
1312 entry =
1313 create_proc_entry("POST", S_IFREG | S_IRUGO | S_IRUSR,
1314 acpi_device_dir(device));
1315 if (!entry)
1316 return -ENODEV;
1317 else {
1318 acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
1319 entry->proc_fops = &acpi_video_bus_POST_fops;
1320 entry->data = acpi_driver_data(device);
1321 entry->owner = THIS_MODULE;
1324 /* 'DOS' [R/W] */
1325 entry =
1326 create_proc_entry("DOS", S_IFREG | S_IRUGO | S_IRUSR,
1327 acpi_device_dir(device));
1328 if (!entry)
1329 return -ENODEV;
1330 else {
1331 acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
1332 entry->proc_fops = &acpi_video_bus_DOS_fops;
1333 entry->data = acpi_driver_data(device);
1334 entry->owner = THIS_MODULE;
1337 return 0;
1340 static int acpi_video_bus_remove_fs(struct acpi_device *device)
1342 struct acpi_video_bus *video;
1345 video = acpi_driver_data(device);
1347 if (acpi_device_dir(device)) {
1348 remove_proc_entry("info", acpi_device_dir(device));
1349 remove_proc_entry("ROM", acpi_device_dir(device));
1350 remove_proc_entry("POST_info", acpi_device_dir(device));
1351 remove_proc_entry("POST", acpi_device_dir(device));
1352 remove_proc_entry("DOS", acpi_device_dir(device));
1353 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1354 acpi_device_dir(device) = NULL;
1357 return 0;
1360 /* --------------------------------------------------------------------------
1361 Driver Interface
1362 -------------------------------------------------------------------------- */
1364 /* device interface */
1365 static struct acpi_video_device_attrib*
1366 acpi_video_get_device_attr(struct acpi_video_bus *video, unsigned long device_id)
1368 int count;
1370 for(count = 0; count < video->attached_count; count++)
1371 if((video->attached_array[count].value.int_val & 0xffff) == device_id)
1372 return &(video->attached_array[count].value.attrib);
1373 return NULL;
1376 static int
1377 acpi_video_bus_get_one_device(struct acpi_device *device,
1378 struct acpi_video_bus *video)
1380 unsigned long device_id;
1381 int status;
1382 struct acpi_video_device *data;
1383 struct acpi_video_device_attrib* attribute;
1385 if (!device || !video)
1386 return -EINVAL;
1388 status =
1389 acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1390 if (ACPI_SUCCESS(status)) {
1392 data = kzalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1393 if (!data)
1394 return -ENOMEM;
1396 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1397 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1398 acpi_driver_data(device) = data;
1400 data->device_id = device_id;
1401 data->video = video;
1402 data->dev = device;
1404 attribute = acpi_video_get_device_attr(video, device_id);
1406 if((attribute != NULL) && attribute->device_id_scheme) {
1407 switch (attribute->display_type) {
1408 case ACPI_VIDEO_DISPLAY_CRT:
1409 data->flags.crt = 1;
1410 break;
1411 case ACPI_VIDEO_DISPLAY_TV:
1412 data->flags.tvout = 1;
1413 break;
1414 case ACPI_VIDEO_DISPLAY_DVI:
1415 data->flags.dvi = 1;
1416 break;
1417 case ACPI_VIDEO_DISPLAY_LCD:
1418 data->flags.lcd = 1;
1419 break;
1420 default:
1421 data->flags.unknown = 1;
1422 break;
1424 if(attribute->bios_can_detect)
1425 data->flags.bios = 1;
1426 } else
1427 data->flags.unknown = 1;
1429 acpi_video_device_bind(video, data);
1430 acpi_video_device_find_cap(data);
1432 status = acpi_install_notify_handler(device->handle,
1433 ACPI_DEVICE_NOTIFY,
1434 acpi_video_device_notify,
1435 data);
1436 if (ACPI_FAILURE(status)) {
1437 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1438 "Error installing notify handler\n"));
1439 if(data->brightness)
1440 kfree(data->brightness->levels);
1441 kfree(data->brightness);
1442 kfree(data);
1443 return -ENODEV;
1446 mutex_lock(&video->device_list_lock);
1447 list_add_tail(&data->entry, &video->video_device_list);
1448 mutex_unlock(&video->device_list_lock);
1450 acpi_video_device_add_fs(device);
1452 return 0;
1455 return -ENOENT;
1459 * Arg:
1460 * video : video bus device
1462 * Return:
1463 * none
1465 * Enumerate the video device list of the video bus,
1466 * bind the ids with the corresponding video devices
1467 * under the video bus.
1470 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1472 struct acpi_video_device *dev;
1474 mutex_lock(&video->device_list_lock);
1476 list_for_each_entry(dev, &video->video_device_list, entry)
1477 acpi_video_device_bind(video, dev);
1479 mutex_unlock(&video->device_list_lock);
1483 * Arg:
1484 * video : video bus device
1485 * device : video output device under the video
1486 * bus
1488 * Return:
1489 * none
1491 * Bind the ids with the corresponding video devices
1492 * under the video bus.
1495 static void
1496 acpi_video_device_bind(struct acpi_video_bus *video,
1497 struct acpi_video_device *device)
1499 int i;
1501 #define IDS_VAL(i) video->attached_array[i].value.int_val
1502 #define IDS_BIND(i) video->attached_array[i].bind_info
1504 for (i = 0; IDS_VAL(i) != ACPI_VIDEO_HEAD_INVALID &&
1505 i < video->attached_count; i++) {
1506 if (device->device_id == (IDS_VAL(i) & 0xffff)) {
1507 IDS_BIND(i) = device;
1508 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1511 #undef IDS_VAL
1512 #undef IDS_BIND
1516 * Arg:
1517 * video : video bus device
1519 * Return:
1520 * < 0 : error
1522 * Call _DOD to enumerate all devices attached to display adapter
1526 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1528 int status;
1529 int count;
1530 int i;
1531 struct acpi_video_enumerated_device *active_device_list;
1532 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1533 union acpi_object *dod = NULL;
1534 union acpi_object *obj;
1536 status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1537 if (!ACPI_SUCCESS(status)) {
1538 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1539 return status;
1542 dod = buffer.pointer;
1543 if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1544 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1545 status = -EFAULT;
1546 goto out;
1549 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1550 dod->package.count));
1552 active_device_list = kmalloc((1 +
1553 dod->package.count) *
1554 sizeof(struct
1555 acpi_video_enumerated_device),
1556 GFP_KERNEL);
1558 if (!active_device_list) {
1559 status = -ENOMEM;
1560 goto out;
1563 count = 0;
1564 for (i = 0; i < dod->package.count; i++) {
1565 obj = &dod->package.elements[i];
1567 if (obj->type != ACPI_TYPE_INTEGER) {
1568 printk(KERN_ERR PREFIX "Invalid _DOD data\n");
1569 active_device_list[i].value.int_val =
1570 ACPI_VIDEO_HEAD_INVALID;
1572 active_device_list[i].value.int_val = obj->integer.value;
1573 active_device_list[i].bind_info = NULL;
1574 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1575 (int)obj->integer.value));
1576 count++;
1578 active_device_list[count].value.int_val = ACPI_VIDEO_HEAD_END;
1580 kfree(video->attached_array);
1582 video->attached_array = active_device_list;
1583 video->attached_count = count;
1584 out:
1585 kfree(buffer.pointer);
1586 return status;
1590 * Arg:
1591 * video : video bus device
1592 * event : notify event
1594 * Return:
1595 * < 0 : error
1597 * 1. Find out the current active output device.
1598 * 2. Identify the next output device to switch to.
1599 * 3. call _DSS to do actual switch.
1602 static int acpi_video_switch_output(struct acpi_video_bus *video, int event)
1604 struct list_head *node;
1605 struct acpi_video_device *dev = NULL;
1606 struct acpi_video_device *dev_next = NULL;
1607 struct acpi_video_device *dev_prev = NULL;
1608 unsigned long state;
1609 int status = 0;
1611 mutex_lock(&video->device_list_lock);
1613 list_for_each(node, &video->video_device_list) {
1614 dev = container_of(node, struct acpi_video_device, entry);
1615 status = acpi_video_device_get_state(dev, &state);
1616 if (state & 0x2) {
1617 dev_next = container_of(node->next,
1618 struct acpi_video_device, entry);
1619 dev_prev = container_of(node->prev,
1620 struct acpi_video_device, entry);
1621 goto out;
1625 dev_next = container_of(node->next, struct acpi_video_device, entry);
1626 dev_prev = container_of(node->prev, struct acpi_video_device, entry);
1628 out:
1629 mutex_unlock(&video->device_list_lock);
1631 switch (event) {
1632 case ACPI_VIDEO_NOTIFY_CYCLE:
1633 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:
1634 acpi_video_device_set_state(dev, 0);
1635 acpi_video_device_set_state(dev_next, 0x80000001);
1636 break;
1637 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:
1638 acpi_video_device_set_state(dev, 0);
1639 acpi_video_device_set_state(dev_prev, 0x80000001);
1640 default:
1641 break;
1644 return status;
1647 static int
1648 acpi_video_get_next_level(struct acpi_video_device *device,
1649 u32 level_current, u32 event)
1651 int min, max, min_above, max_below, i, l, delta = 255;
1652 max = max_below = 0;
1653 min = min_above = 255;
1654 /* Find closest level to level_current */
1655 for (i = 0; i < device->brightness->count; i++) {
1656 l = device->brightness->levels[i];
1657 if (abs(l - level_current) < abs(delta)) {
1658 delta = l - level_current;
1659 if (!delta)
1660 break;
1663 /* Ajust level_current to closest available level */
1664 level_current += delta;
1665 for (i = 0; i < device->brightness->count; i++) {
1666 l = device->brightness->levels[i];
1667 if (l < min)
1668 min = l;
1669 if (l > max)
1670 max = l;
1671 if (l < min_above && l > level_current)
1672 min_above = l;
1673 if (l > max_below && l < level_current)
1674 max_below = l;
1677 switch (event) {
1678 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:
1679 return (level_current < max) ? min_above : min;
1680 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:
1681 return (level_current < max) ? min_above : max;
1682 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:
1683 return (level_current > min) ? max_below : min;
1684 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS:
1685 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:
1686 return 0;
1687 default:
1688 return level_current;
1692 static void
1693 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1695 unsigned long level_current, level_next;
1696 acpi_video_device_lcd_get_level_current(device, &level_current);
1697 level_next = acpi_video_get_next_level(device, level_current, event);
1698 acpi_video_device_lcd_set_level(device, level_next);
1701 static int
1702 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1703 struct acpi_device *device)
1705 int status = 0;
1706 struct acpi_device *dev;
1708 acpi_video_device_enumerate(video);
1710 list_for_each_entry(dev, &device->children, node) {
1712 status = acpi_video_bus_get_one_device(dev, video);
1713 if (ACPI_FAILURE(status)) {
1714 ACPI_EXCEPTION((AE_INFO, status, "Cant attach device"));
1715 continue;
1718 return status;
1721 static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1723 acpi_status status;
1724 struct acpi_video_bus *video;
1727 if (!device || !device->video)
1728 return -ENOENT;
1730 video = device->video;
1732 acpi_video_device_remove_fs(device->dev);
1734 status = acpi_remove_notify_handler(device->dev->handle,
1735 ACPI_DEVICE_NOTIFY,
1736 acpi_video_device_notify);
1737 backlight_device_unregister(device->backlight);
1738 video_output_unregister(device->output_dev);
1740 return 0;
1743 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1745 int status;
1746 struct acpi_video_device *dev, *next;
1748 mutex_lock(&video->device_list_lock);
1750 list_for_each_entry_safe(dev, next, &video->video_device_list, entry) {
1752 status = acpi_video_bus_put_one_device(dev);
1753 if (ACPI_FAILURE(status))
1754 printk(KERN_WARNING PREFIX
1755 "hhuuhhuu bug in acpi video driver.\n");
1757 if (dev->brightness) {
1758 kfree(dev->brightness->levels);
1759 kfree(dev->brightness);
1761 list_del(&dev->entry);
1762 kfree(dev);
1765 mutex_unlock(&video->device_list_lock);
1767 return 0;
1770 /* acpi_video interface */
1772 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1774 return acpi_video_bus_DOS(video, 0, 0);
1777 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1779 return acpi_video_bus_DOS(video, 0, 1);
1782 static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
1784 struct acpi_video_bus *video = data;
1785 struct acpi_device *device = NULL;
1786 struct input_dev *input;
1787 int keycode;
1789 if (!video)
1790 return;
1792 device = video->device;
1793 input = video->input;
1795 switch (event) {
1796 case ACPI_VIDEO_NOTIFY_SWITCH: /* User requested a switch,
1797 * most likely via hotkey. */
1798 acpi_bus_generate_proc_event(device, event, 0);
1799 keycode = KEY_SWITCHVIDEOMODE;
1800 break;
1802 case ACPI_VIDEO_NOTIFY_PROBE: /* User plugged in or removed a video
1803 * connector. */
1804 acpi_video_device_enumerate(video);
1805 acpi_video_device_rebind(video);
1806 acpi_video_switch_output(video, event);
1807 acpi_bus_generate_proc_event(device, event, 0);
1808 keycode = KEY_SWITCHVIDEOMODE;
1809 break;
1811 case ACPI_VIDEO_NOTIFY_CYCLE: /* Cycle Display output hotkey pressed. */
1812 acpi_video_switch_output(video, event);
1813 acpi_bus_generate_proc_event(device, event, 0);
1814 keycode = KEY_SWITCHVIDEOMODE;
1815 break;
1816 case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT: /* Next Display output hotkey pressed. */
1817 acpi_video_switch_output(video, event);
1818 acpi_bus_generate_proc_event(device, event, 0);
1819 keycode = KEY_VIDEO_NEXT;
1820 break;
1821 case ACPI_VIDEO_NOTIFY_PREV_OUTPUT: /* previous Display output hotkey pressed. */
1822 acpi_video_switch_output(video, event);
1823 acpi_bus_generate_proc_event(device, event, 0);
1824 keycode = KEY_VIDEO_PREV;
1825 break;
1827 default:
1828 keycode = KEY_UNKNOWN;
1829 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1830 "Unsupported event [0x%x]\n", event));
1831 break;
1834 input_report_key(input, keycode, 1);
1835 input_sync(input);
1836 input_report_key(input, keycode, 0);
1837 input_sync(input);
1839 return;
1842 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1844 struct acpi_video_device *video_device = data;
1845 struct acpi_device *device = NULL;
1846 struct acpi_video_bus *bus;
1847 struct input_dev *input;
1848 int keycode;
1850 if (!video_device)
1851 return;
1853 device = video_device->dev;
1854 bus = video_device->video;
1855 input = bus->input;
1857 switch (event) {
1858 case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS: /* Cycle brightness */
1859 acpi_video_switch_brightness(video_device, event);
1860 acpi_bus_generate_proc_event(device, event, 0);
1861 keycode = KEY_BRIGHTNESS_CYCLE;
1862 break;
1863 case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS: /* Increase brightness */
1864 acpi_video_switch_brightness(video_device, event);
1865 acpi_bus_generate_proc_event(device, event, 0);
1866 keycode = KEY_BRIGHTNESSUP;
1867 break;
1868 case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS: /* Decrease brightness */
1869 acpi_video_switch_brightness(video_device, event);
1870 acpi_bus_generate_proc_event(device, event, 0);
1871 keycode = KEY_BRIGHTNESSDOWN;
1872 break;
1873 case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
1874 acpi_video_switch_brightness(video_device, event);
1875 acpi_bus_generate_proc_event(device, event, 0);
1876 keycode = KEY_BRIGHTNESS_ZERO;
1877 break;
1878 case ACPI_VIDEO_NOTIFY_DISPLAY_OFF: /* display device off */
1879 acpi_video_switch_brightness(video_device, event);
1880 acpi_bus_generate_proc_event(device, event, 0);
1881 keycode = KEY_DISPLAY_OFF;
1882 break;
1883 default:
1884 keycode = KEY_UNKNOWN;
1885 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1886 "Unsupported event [0x%x]\n", event));
1887 break;
1890 input_report_key(input, keycode, 1);
1891 input_sync(input);
1892 input_report_key(input, keycode, 0);
1893 input_sync(input);
1895 return;
1898 static int instance;
1899 static int acpi_video_bus_add(struct acpi_device *device)
1901 acpi_status status;
1902 struct acpi_video_bus *video;
1903 struct input_dev *input;
1904 int error;
1906 video = kzalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1907 if (!video)
1908 return -ENOMEM;
1910 /* a hack to fix the duplicate name "VID" problem on T61 */
1911 if (!strcmp(device->pnp.bus_id, "VID")) {
1912 if (instance)
1913 device->pnp.bus_id[3] = '0' + instance;
1914 instance ++;
1917 video->device = device;
1918 strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1919 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1920 acpi_driver_data(device) = video;
1922 acpi_video_bus_find_cap(video);
1923 error = acpi_video_bus_check(video);
1924 if (error)
1925 goto err_free_video;
1927 error = acpi_video_bus_add_fs(device);
1928 if (error)
1929 goto err_free_video;
1931 mutex_init(&video->device_list_lock);
1932 INIT_LIST_HEAD(&video->video_device_list);
1934 acpi_video_bus_get_devices(video, device);
1935 acpi_video_bus_start_devices(video);
1937 status = acpi_install_notify_handler(device->handle,
1938 ACPI_DEVICE_NOTIFY,
1939 acpi_video_bus_notify, video);
1940 if (ACPI_FAILURE(status)) {
1941 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1942 "Error installing notify handler\n"));
1943 error = -ENODEV;
1944 goto err_stop_video;
1947 video->input = input = input_allocate_device();
1948 if (!input) {
1949 error = -ENOMEM;
1950 goto err_uninstall_notify;
1953 snprintf(video->phys, sizeof(video->phys),
1954 "%s/video/input0", acpi_device_hid(video->device));
1956 input->name = acpi_device_name(video->device);
1957 input->phys = video->phys;
1958 input->id.bustype = BUS_HOST;
1959 input->id.product = 0x06;
1960 input->dev.parent = &device->dev;
1961 input->evbit[0] = BIT(EV_KEY);
1962 set_bit(KEY_SWITCHVIDEOMODE, input->keybit);
1963 set_bit(KEY_VIDEO_NEXT, input->keybit);
1964 set_bit(KEY_VIDEO_PREV, input->keybit);
1965 set_bit(KEY_BRIGHTNESS_CYCLE, input->keybit);
1966 set_bit(KEY_BRIGHTNESSUP, input->keybit);
1967 set_bit(KEY_BRIGHTNESSDOWN, input->keybit);
1968 set_bit(KEY_BRIGHTNESS_ZERO, input->keybit);
1969 set_bit(KEY_DISPLAY_OFF, input->keybit);
1970 set_bit(KEY_UNKNOWN, input->keybit);
1972 error = input_register_device(input);
1973 if (error)
1974 goto err_free_input_dev;
1976 printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s rom: %s post: %s)\n",
1977 ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1978 video->flags.multihead ? "yes" : "no",
1979 video->flags.rom ? "yes" : "no",
1980 video->flags.post ? "yes" : "no");
1982 return 0;
1984 err_free_input_dev:
1985 input_free_device(input);
1986 err_uninstall_notify:
1987 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
1988 acpi_video_bus_notify);
1989 err_stop_video:
1990 acpi_video_bus_stop_devices(video);
1991 acpi_video_bus_put_devices(video);
1992 kfree(video->attached_array);
1993 acpi_video_bus_remove_fs(device);
1994 err_free_video:
1995 kfree(video);
1996 acpi_driver_data(device) = NULL;
1998 return error;
2001 static int acpi_video_bus_remove(struct acpi_device *device, int type)
2003 acpi_status status = 0;
2004 struct acpi_video_bus *video = NULL;
2007 if (!device || !acpi_driver_data(device))
2008 return -EINVAL;
2010 video = acpi_driver_data(device);
2012 acpi_video_bus_stop_devices(video);
2014 status = acpi_remove_notify_handler(video->device->handle,
2015 ACPI_DEVICE_NOTIFY,
2016 acpi_video_bus_notify);
2018 acpi_video_bus_put_devices(video);
2019 acpi_video_bus_remove_fs(device);
2021 input_unregister_device(video->input);
2022 kfree(video->attached_array);
2023 kfree(video);
2025 return 0;
2028 static int __init acpi_video_init(void)
2030 int result = 0;
2034 acpi_dbg_level = 0xFFFFFFFF;
2035 acpi_dbg_layer = 0x08000000;
2038 acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
2039 if (!acpi_video_dir)
2040 return -ENODEV;
2041 acpi_video_dir->owner = THIS_MODULE;
2043 result = acpi_bus_register_driver(&acpi_video_bus);
2044 if (result < 0) {
2045 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2046 return -ENODEV;
2049 return 0;
2052 static void __exit acpi_video_exit(void)
2055 acpi_bus_unregister_driver(&acpi_video_bus);
2057 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
2059 return;
2062 module_init(acpi_video_init);
2063 module_exit(acpi_video_exit);