]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/acpi/video.c
ACPI: Set fake hid for non-PNPID ACPI devices
[linux-2.6-omap-h63xx.git] / drivers / acpi / video.c
1 /*
2  *  video.c - ACPI Video Driver ($Revision:$)
3  *
4  *  Copyright (C) 2004 Luming Yu <luming.yu@intel.com>
5  *  Copyright (C) 2004 Bruno Ducrot <ducrot@poupinou.org>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/list.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33
34 #include <asm/uaccess.h>
35
36 #include <acpi/acpi_bus.h>
37 #include <acpi/acpi_drivers.h>
38
39 #define ACPI_VIDEO_COMPONENT            0x08000000
40 #define ACPI_VIDEO_CLASS                "video"
41 #define ACPI_VIDEO_DRIVER_NAME          "ACPI Video Driver"
42 #define ACPI_VIDEO_BUS_NAME             "Video Bus"
43 #define ACPI_VIDEO_DEVICE_NAME          "Video Device"
44 #define ACPI_VIDEO_NOTIFY_SWITCH        0x80
45 #define ACPI_VIDEO_NOTIFY_PROBE         0x81
46 #define ACPI_VIDEO_NOTIFY_CYCLE         0x82
47 #define ACPI_VIDEO_NOTIFY_NEXT_OUTPUT   0x83
48 #define ACPI_VIDEO_NOTIFY_PREV_OUTPUT   0x84
49
50 #define ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS      0x82
51 #define ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS        0x83
52 #define ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS        0x84
53 #define ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS       0x85
54 #define ACPI_VIDEO_NOTIFY_DISPLAY_OFF           0x86
55
56 #define ACPI_VIDEO_HEAD_INVALID         (~0u - 1)
57 #define ACPI_VIDEO_HEAD_END             (~0u)
58
59 #define _COMPONENT              ACPI_VIDEO_COMPONENT
60 ACPI_MODULE_NAME("acpi_video")
61
62     MODULE_AUTHOR("Bruno Ducrot");
63 MODULE_DESCRIPTION(ACPI_VIDEO_DRIVER_NAME);
64 MODULE_LICENSE("GPL");
65
66 static int acpi_video_bus_add(struct acpi_device *device);
67 static int acpi_video_bus_remove(struct acpi_device *device, int type);
68
69 static struct acpi_driver acpi_video_bus = {
70         .name = ACPI_VIDEO_DRIVER_NAME,
71         .class = ACPI_VIDEO_CLASS,
72         .ids = ACPI_VIDEO_HID,
73         .ops = {
74                 .add = acpi_video_bus_add,
75                 .remove = acpi_video_bus_remove,
76                 },
77 };
78
79 struct acpi_video_bus_flags {
80         u8 multihead:1;         /* can switch video heads */
81         u8 rom:1;               /* can retrieve a video rom */
82         u8 post:1;              /* can configure the head to */
83         u8 reserved:5;
84 };
85
86 struct acpi_video_bus_cap {
87         u8 _DOS:1;              /*Enable/Disable output switching */
88         u8 _DOD:1;              /*Enumerate all devices attached to display adapter */
89         u8 _ROM:1;              /*Get ROM Data */
90         u8 _GPD:1;              /*Get POST Device */
91         u8 _SPD:1;              /*Set POST Device */
92         u8 _VPO:1;              /*Video POST Options */
93         u8 reserved:2;
94 };
95
96 struct acpi_video_device_attrib {
97         u32 display_index:4;    /* A zero-based instance of the Display */
98         u32 display_port_attachment:4;  /*This field differenates displays type */
99         u32 display_type:4;     /*Describe the specific type in use */
100         u32 vendor_specific:4;  /*Chipset Vendor Specifi */
101         u32 bios_can_detect:1;  /*BIOS can detect the device */
102         u32 depend_on_vga:1;    /*Non-VGA output device whose power is related to 
103                                    the VGA device. */
104         u32 pipe_id:3;          /*For VGA multiple-head devices. */
105         u32 reserved:10;        /*Must be 0 */
106         u32 device_id_scheme:1; /*Device ID Scheme */
107 };
108
109 struct acpi_video_enumerated_device {
110         union {
111                 u32 int_val;
112                 struct acpi_video_device_attrib attrib;
113         } value;
114         struct acpi_video_device *bind_info;
115 };
116
117 struct acpi_video_bus {
118         struct acpi_device *device;
119         u8 dos_setting;
120         struct acpi_video_enumerated_device *attached_array;
121         u8 attached_count;
122         struct acpi_video_bus_cap cap;
123         struct acpi_video_bus_flags flags;
124         struct semaphore sem;
125         struct list_head video_device_list;
126         struct proc_dir_entry *dir;
127 };
128
129 struct acpi_video_device_flags {
130         u8 crt:1;
131         u8 lcd:1;
132         u8 tvout:1;
133         u8 bios:1;
134         u8 unknown:1;
135         u8 reserved:3;
136 };
137
138 struct acpi_video_device_cap {
139         u8 _ADR:1;              /*Return the unique ID */
140         u8 _BCL:1;              /*Query list of brightness control levels supported */
141         u8 _BCM:1;              /*Set the brightness level */
142         u8 _DDC:1;              /*Return the EDID for this device */
143         u8 _DCS:1;              /*Return status of output device */
144         u8 _DGS:1;              /*Query graphics state */
145         u8 _DSS:1;              /*Device state set */
146         u8 _reserved:1;
147 };
148
149 struct acpi_video_device_brightness {
150         int curr;
151         int count;
152         int *levels;
153 };
154
155 struct acpi_video_device {
156         unsigned long device_id;
157         struct acpi_video_device_flags flags;
158         struct acpi_video_device_cap cap;
159         struct list_head entry;
160         struct acpi_video_bus *video;
161         struct acpi_device *dev;
162         struct acpi_video_device_brightness *brightness;
163 };
164
165 /* bus */
166 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file);
167 static struct file_operations acpi_video_bus_info_fops = {
168         .open = acpi_video_bus_info_open_fs,
169         .read = seq_read,
170         .llseek = seq_lseek,
171         .release = single_release,
172 };
173
174 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file);
175 static struct file_operations acpi_video_bus_ROM_fops = {
176         .open = acpi_video_bus_ROM_open_fs,
177         .read = seq_read,
178         .llseek = seq_lseek,
179         .release = single_release,
180 };
181
182 static int acpi_video_bus_POST_info_open_fs(struct inode *inode,
183                                             struct file *file);
184 static struct file_operations acpi_video_bus_POST_info_fops = {
185         .open = acpi_video_bus_POST_info_open_fs,
186         .read = seq_read,
187         .llseek = seq_lseek,
188         .release = single_release,
189 };
190
191 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file);
192 static struct file_operations acpi_video_bus_POST_fops = {
193         .open = acpi_video_bus_POST_open_fs,
194         .read = seq_read,
195         .llseek = seq_lseek,
196         .release = single_release,
197 };
198
199 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file);
200 static struct file_operations acpi_video_bus_DOS_fops = {
201         .open = acpi_video_bus_DOS_open_fs,
202         .read = seq_read,
203         .llseek = seq_lseek,
204         .release = single_release,
205 };
206
207 /* device */
208 static int acpi_video_device_info_open_fs(struct inode *inode,
209                                           struct file *file);
210 static struct file_operations acpi_video_device_info_fops = {
211         .open = acpi_video_device_info_open_fs,
212         .read = seq_read,
213         .llseek = seq_lseek,
214         .release = single_release,
215 };
216
217 static int acpi_video_device_state_open_fs(struct inode *inode,
218                                            struct file *file);
219 static struct file_operations acpi_video_device_state_fops = {
220         .open = acpi_video_device_state_open_fs,
221         .read = seq_read,
222         .llseek = seq_lseek,
223         .release = single_release,
224 };
225
226 static int acpi_video_device_brightness_open_fs(struct inode *inode,
227                                                 struct file *file);
228 static struct file_operations acpi_video_device_brightness_fops = {
229         .open = acpi_video_device_brightness_open_fs,
230         .read = seq_read,
231         .llseek = seq_lseek,
232         .release = single_release,
233 };
234
235 static int acpi_video_device_EDID_open_fs(struct inode *inode,
236                                           struct file *file);
237 static struct file_operations acpi_video_device_EDID_fops = {
238         .open = acpi_video_device_EDID_open_fs,
239         .read = seq_read,
240         .llseek = seq_lseek,
241         .release = single_release,
242 };
243
244 static char device_decode[][30] = {
245         "motherboard VGA device",
246         "PCI VGA device",
247         "AGP VGA device",
248         "UNKNOWN",
249 };
250
251 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data);
252 static void acpi_video_device_rebind(struct acpi_video_bus *video);
253 static void acpi_video_device_bind(struct acpi_video_bus *video,
254                                    struct acpi_video_device *device);
255 static int acpi_video_device_enumerate(struct acpi_video_bus *video);
256 static int acpi_video_switch_output(struct acpi_video_bus *video, int event);
257 static int acpi_video_get_next_level(struct acpi_video_device *device,
258                                      u32 level_current, u32 event);
259 static void acpi_video_switch_brightness(struct acpi_video_device *device,
260                                          int event);
261
262 /* --------------------------------------------------------------------------
263                                Video Management
264    -------------------------------------------------------------------------- */
265
266 /* device */
267
268 static int
269 acpi_video_device_query(struct acpi_video_device *device, unsigned long *state)
270 {
271         int status;
272
273         status = acpi_evaluate_integer(device->dev->handle, "_DGS", NULL, state);
274
275         return status;
276 }
277
278 static int
279 acpi_video_device_get_state(struct acpi_video_device *device,
280                             unsigned long *state)
281 {
282         int status;
283
284         status = acpi_evaluate_integer(device->dev->handle, "_DCS", NULL, state);
285
286         return status;
287 }
288
289 static int
290 acpi_video_device_set_state(struct acpi_video_device *device, int state)
291 {
292         int status;
293         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
294         struct acpi_object_list args = { 1, &arg0 };
295         unsigned long ret;
296
297
298         arg0.integer.value = state;
299         status = acpi_evaluate_integer(device->dev->handle, "_DSS", &args, &ret);
300
301         return status;
302 }
303
304 static int
305 acpi_video_device_lcd_query_levels(struct acpi_video_device *device,
306                                    union acpi_object **levels)
307 {
308         int status;
309         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
310         union acpi_object *obj;
311
312
313         *levels = NULL;
314
315         status = acpi_evaluate_object(device->dev->handle, "_BCL", NULL, &buffer);
316         if (!ACPI_SUCCESS(status))
317                 return status;
318         obj = (union acpi_object *)buffer.pointer;
319         if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
320                 printk(KERN_ERR PREFIX "Invalid _BCL data\n");
321                 status = -EFAULT;
322                 goto err;
323         }
324
325         *levels = obj;
326
327         return 0;
328
329       err:
330         kfree(buffer.pointer);
331
332         return status;
333 }
334
335 static int
336 acpi_video_device_lcd_set_level(struct acpi_video_device *device, int level)
337 {
338         int status;
339         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
340         struct acpi_object_list args = { 1, &arg0 };
341
342
343         arg0.integer.value = level;
344         status = acpi_evaluate_object(device->dev->handle, "_BCM", &args, NULL);
345
346         printk(KERN_DEBUG "set_level status: %x\n", status);
347         return status;
348 }
349
350 static int
351 acpi_video_device_lcd_get_level_current(struct acpi_video_device *device,
352                                         unsigned long *level)
353 {
354         int status;
355
356         status = acpi_evaluate_integer(device->dev->handle, "_BQC", NULL, level);
357
358         return status;
359 }
360
361 static int
362 acpi_video_device_EDID(struct acpi_video_device *device,
363                        union acpi_object **edid, ssize_t length)
364 {
365         int status;
366         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
367         union acpi_object *obj;
368         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
369         struct acpi_object_list args = { 1, &arg0 };
370
371
372         *edid = NULL;
373
374         if (!device)
375                 return -ENODEV;
376         if (length == 128)
377                 arg0.integer.value = 1;
378         else if (length == 256)
379                 arg0.integer.value = 2;
380         else
381                 return -EINVAL;
382
383         status = acpi_evaluate_object(device->dev->handle, "_DDC", &args, &buffer);
384         if (ACPI_FAILURE(status))
385                 return -ENODEV;
386
387         obj = (union acpi_object *)buffer.pointer;
388
389         if (obj && obj->type == ACPI_TYPE_BUFFER)
390                 *edid = obj;
391         else {
392                 printk(KERN_ERR PREFIX "Invalid _DDC data\n");
393                 status = -EFAULT;
394                 kfree(obj);
395         }
396
397         return status;
398 }
399
400 /* bus */
401
402 static int
403 acpi_video_bus_set_POST(struct acpi_video_bus *video, unsigned long option)
404 {
405         int status;
406         unsigned long tmp;
407         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
408         struct acpi_object_list args = { 1, &arg0 };
409
410
411         arg0.integer.value = option;
412
413         status = acpi_evaluate_integer(video->device->handle, "_SPD", &args, &tmp);
414         if (ACPI_SUCCESS(status))
415                 status = tmp ? (-EINVAL) : (AE_OK);
416
417         return status;
418 }
419
420 static int
421 acpi_video_bus_get_POST(struct acpi_video_bus *video, unsigned long *id)
422 {
423         int status;
424
425         status = acpi_evaluate_integer(video->device->handle, "_GPD", NULL, id);
426
427         return status;
428 }
429
430 static int
431 acpi_video_bus_POST_options(struct acpi_video_bus *video,
432                             unsigned long *options)
433 {
434         int status;
435
436         status = acpi_evaluate_integer(video->device->handle, "_VPO", NULL, options);
437         *options &= 3;
438
439         return status;
440 }
441
442 /*
443  *  Arg:
444  *      video           : video bus device pointer
445  *      bios_flag       : 
446  *              0.      The system BIOS should NOT automatically switch(toggle)
447  *                      the active display output.
448  *              1.      The system BIOS should automatically switch (toggle) the
449  *                      active display output. No swich event.
450  *              2.      The _DGS value should be locked.
451  *              3.      The system BIOS should not automatically switch (toggle) the
452  *                      active display output, but instead generate the display switch
453  *                      event notify code.
454  *      lcd_flag        :
455  *              0.      The system BIOS should automatically control the brightness level
456  *                      of the LCD, when the power changes from AC to DC
457  *              1.      The system BIOS should NOT automatically control the brightness 
458  *                      level of the LCD, when the power changes from AC to DC.
459  * Return Value:
460  *              -1      wrong arg.
461  */
462
463 static int
464 acpi_video_bus_DOS(struct acpi_video_bus *video, int bios_flag, int lcd_flag)
465 {
466         acpi_integer status = 0;
467         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
468         struct acpi_object_list args = { 1, &arg0 };
469
470
471         if (bios_flag < 0 || bios_flag > 3 || lcd_flag < 0 || lcd_flag > 1) {
472                 status = -1;
473                 goto Failed;
474         }
475         arg0.integer.value = (lcd_flag << 2) | bios_flag;
476         video->dos_setting = arg0.integer.value;
477         acpi_evaluate_object(video->device->handle, "_DOS", &args, NULL);
478
479       Failed:
480         return status;
481 }
482
483 /*
484  *  Arg:        
485  *      device  : video output device (LCD, CRT, ..)
486  *
487  *  Return Value:
488  *      None
489  *
490  *  Find out all required AML method defined under the output
491  *  device.
492  */
493
494 static void acpi_video_device_find_cap(struct acpi_video_device *device)
495 {
496         acpi_integer status;
497         acpi_handle h_dummy1;
498         int i;
499         union acpi_object *obj = NULL;
500         struct acpi_video_device_brightness *br = NULL;
501
502
503         memset(&device->cap, 0, 4);
504
505         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_ADR", &h_dummy1))) {
506                 device->cap._ADR = 1;
507         }
508         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCL", &h_dummy1))) {
509                 device->cap._BCL = 1;
510         }
511         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_BCM", &h_dummy1))) {
512                 device->cap._BCM = 1;
513         }
514         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DDC", &h_dummy1))) {
515                 device->cap._DDC = 1;
516         }
517         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DCS", &h_dummy1))) {
518                 device->cap._DCS = 1;
519         }
520         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DGS", &h_dummy1))) {
521                 device->cap._DGS = 1;
522         }
523         if (ACPI_SUCCESS(acpi_get_handle(device->dev->handle, "_DSS", &h_dummy1))) {
524                 device->cap._DSS = 1;
525         }
526
527         status = acpi_video_device_lcd_query_levels(device, &obj);
528
529         if (obj && obj->type == ACPI_TYPE_PACKAGE && obj->package.count >= 2) {
530                 int count = 0;
531                 union acpi_object *o;
532
533                 br = kmalloc(sizeof(*br), GFP_KERNEL);
534                 if (!br) {
535                         printk(KERN_ERR "can't allocate memory\n");
536                 } else {
537                         memset(br, 0, sizeof(*br));
538                         br->levels = kmalloc(obj->package.count *
539                                              sizeof *(br->levels), GFP_KERNEL);
540                         if (!br->levels)
541                                 goto out;
542
543                         for (i = 0; i < obj->package.count; i++) {
544                                 o = (union acpi_object *)&obj->package.
545                                     elements[i];
546                                 if (o->type != ACPI_TYPE_INTEGER) {
547                                         printk(KERN_ERR PREFIX "Invalid data\n");
548                                         continue;
549                                 }
550                                 br->levels[count] = (u32) o->integer.value;
551                                 count++;
552                         }
553                       out:
554                         if (count < 2) {
555                                 kfree(br->levels);
556                                 kfree(br);
557                         } else {
558                                 br->count = count;
559                                 device->brightness = br;
560                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
561                                                   "found %d brightness levels\n",
562                                                   count));
563                         }
564                 }
565         }
566
567         kfree(obj);
568
569         return;
570 }
571
572 /*
573  *  Arg:        
574  *      device  : video output device (VGA)
575  *
576  *  Return Value:
577  *      None
578  *
579  *  Find out all required AML method defined under the video bus device.
580  */
581
582 static void acpi_video_bus_find_cap(struct acpi_video_bus *video)
583 {
584         acpi_handle h_dummy1;
585
586         memset(&video->cap, 0, 4);
587         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOS", &h_dummy1))) {
588                 video->cap._DOS = 1;
589         }
590         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_DOD", &h_dummy1))) {
591                 video->cap._DOD = 1;
592         }
593         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_ROM", &h_dummy1))) {
594                 video->cap._ROM = 1;
595         }
596         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_GPD", &h_dummy1))) {
597                 video->cap._GPD = 1;
598         }
599         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_SPD", &h_dummy1))) {
600                 video->cap._SPD = 1;
601         }
602         if (ACPI_SUCCESS(acpi_get_handle(video->device->handle, "_VPO", &h_dummy1))) {
603                 video->cap._VPO = 1;
604         }
605 }
606
607 /*
608  * Check whether the video bus device has required AML method to
609  * support the desired features
610  */
611
612 static int acpi_video_bus_check(struct acpi_video_bus *video)
613 {
614         acpi_status status = -ENOENT;
615
616
617         if (!video)
618                 return -EINVAL;
619
620         /* Since there is no HID, CID and so on for VGA driver, we have
621          * to check well known required nodes.
622          */
623
624         /* Does this device able to support video switching ? */
625         if (video->cap._DOS) {
626                 video->flags.multihead = 1;
627                 status = 0;
628         }
629
630         /* Does this device able to retrieve a retrieve a video ROM ? */
631         if (video->cap._ROM) {
632                 video->flags.rom = 1;
633                 status = 0;
634         }
635
636         /* Does this device able to configure which video device to POST ? */
637         if (video->cap._GPD && video->cap._SPD && video->cap._VPO) {
638                 video->flags.post = 1;
639                 status = 0;
640         }
641
642         return status;
643 }
644
645 /* --------------------------------------------------------------------------
646                               FS Interface (/proc)
647    -------------------------------------------------------------------------- */
648
649 static struct proc_dir_entry *acpi_video_dir;
650
651 /* video devices */
652
653 static int acpi_video_device_info_seq_show(struct seq_file *seq, void *offset)
654 {
655         struct acpi_video_device *dev =
656             (struct acpi_video_device *)seq->private;
657
658
659         if (!dev)
660                 goto end;
661
662         seq_printf(seq, "device_id:    0x%04x\n", (u32) dev->device_id);
663         seq_printf(seq, "type:         ");
664         if (dev->flags.crt)
665                 seq_printf(seq, "CRT\n");
666         else if (dev->flags.lcd)
667                 seq_printf(seq, "LCD\n");
668         else if (dev->flags.tvout)
669                 seq_printf(seq, "TVOUT\n");
670         else
671                 seq_printf(seq, "UNKNOWN\n");
672
673         seq_printf(seq, "known by bios: %s\n", dev->flags.bios ? "yes" : "no");
674
675       end:
676         return 0;
677 }
678
679 static int
680 acpi_video_device_info_open_fs(struct inode *inode, struct file *file)
681 {
682         return single_open(file, acpi_video_device_info_seq_show,
683                            PDE(inode)->data);
684 }
685
686 static int acpi_video_device_state_seq_show(struct seq_file *seq, void *offset)
687 {
688         int status;
689         struct acpi_video_device *dev =
690             (struct acpi_video_device *)seq->private;
691         unsigned long state;
692
693
694         if (!dev)
695                 goto end;
696
697         status = acpi_video_device_get_state(dev, &state);
698         seq_printf(seq, "state:     ");
699         if (ACPI_SUCCESS(status))
700                 seq_printf(seq, "0x%02lx\n", state);
701         else
702                 seq_printf(seq, "<not supported>\n");
703
704         status = acpi_video_device_query(dev, &state);
705         seq_printf(seq, "query:     ");
706         if (ACPI_SUCCESS(status))
707                 seq_printf(seq, "0x%02lx\n", state);
708         else
709                 seq_printf(seq, "<not supported>\n");
710
711       end:
712         return 0;
713 }
714
715 static int
716 acpi_video_device_state_open_fs(struct inode *inode, struct file *file)
717 {
718         return single_open(file, acpi_video_device_state_seq_show,
719                            PDE(inode)->data);
720 }
721
722 static ssize_t
723 acpi_video_device_write_state(struct file *file,
724                               const char __user * buffer,
725                               size_t count, loff_t * data)
726 {
727         int status;
728         struct seq_file *m = (struct seq_file *)file->private_data;
729         struct acpi_video_device *dev = (struct acpi_video_device *)m->private;
730         char str[12] = { 0 };
731         u32 state = 0;
732
733
734         if (!dev || count + 1 > sizeof str)
735                 return -EINVAL;
736
737         if (copy_from_user(str, buffer, count))
738                 return -EFAULT;
739
740         str[count] = 0;
741         state = simple_strtoul(str, NULL, 0);
742         state &= ((1ul << 31) | (1ul << 30) | (1ul << 0));
743
744         status = acpi_video_device_set_state(dev, state);
745
746         if (status)
747                 return -EFAULT;
748
749         return count;
750 }
751
752 static int
753 acpi_video_device_brightness_seq_show(struct seq_file *seq, void *offset)
754 {
755         struct acpi_video_device *dev =
756             (struct acpi_video_device *)seq->private;
757         int i;
758
759
760         if (!dev || !dev->brightness) {
761                 seq_printf(seq, "<not supported>\n");
762                 return 0;
763         }
764
765         seq_printf(seq, "levels: ");
766         for (i = 0; i < dev->brightness->count; i++)
767                 seq_printf(seq, " %d", dev->brightness->levels[i]);
768         seq_printf(seq, "\ncurrent: %d\n", dev->brightness->curr);
769
770         return 0;
771 }
772
773 static int
774 acpi_video_device_brightness_open_fs(struct inode *inode, struct file *file)
775 {
776         return single_open(file, acpi_video_device_brightness_seq_show,
777                            PDE(inode)->data);
778 }
779
780 static ssize_t
781 acpi_video_device_write_brightness(struct file *file,
782                                    const char __user * buffer,
783                                    size_t count, loff_t * data)
784 {
785         struct seq_file *m = (struct seq_file *)file->private_data;
786         struct acpi_video_device *dev = (struct acpi_video_device *)m->private;
787         char str[4] = { 0 };
788         unsigned int level = 0;
789         int i;
790
791
792         if (!dev || !dev->brightness || count + 1 > sizeof str)
793                 return -EINVAL;
794
795         if (copy_from_user(str, buffer, count))
796                 return -EFAULT;
797
798         str[count] = 0;
799         level = simple_strtoul(str, NULL, 0);
800
801         if (level > 100)
802                 return -EFAULT;
803
804         /* validate though the list of available levels */
805         for (i = 0; i < dev->brightness->count; i++)
806                 if (level == dev->brightness->levels[i]) {
807                         if (ACPI_SUCCESS
808                             (acpi_video_device_lcd_set_level(dev, level)))
809                                 dev->brightness->curr = level;
810                         break;
811                 }
812
813         return count;
814 }
815
816 static int acpi_video_device_EDID_seq_show(struct seq_file *seq, void *offset)
817 {
818         struct acpi_video_device *dev =
819             (struct acpi_video_device *)seq->private;
820         int status;
821         int i;
822         union acpi_object *edid = NULL;
823
824
825         if (!dev)
826                 goto out;
827
828         status = acpi_video_device_EDID(dev, &edid, 128);
829         if (ACPI_FAILURE(status)) {
830                 status = acpi_video_device_EDID(dev, &edid, 256);
831         }
832
833         if (ACPI_FAILURE(status)) {
834                 goto out;
835         }
836
837         if (edid && edid->type == ACPI_TYPE_BUFFER) {
838                 for (i = 0; i < edid->buffer.length; i++)
839                         seq_putc(seq, edid->buffer.pointer[i]);
840         }
841
842       out:
843         if (!edid)
844                 seq_printf(seq, "<not supported>\n");
845         else
846                 kfree(edid);
847
848         return 0;
849 }
850
851 static int
852 acpi_video_device_EDID_open_fs(struct inode *inode, struct file *file)
853 {
854         return single_open(file, acpi_video_device_EDID_seq_show,
855                            PDE(inode)->data);
856 }
857
858 static int acpi_video_device_add_fs(struct acpi_device *device)
859 {
860         struct proc_dir_entry *entry = NULL;
861         struct acpi_video_device *vid_dev;
862
863
864         if (!device)
865                 return -ENODEV;
866
867         vid_dev = (struct acpi_video_device *)acpi_driver_data(device);
868         if (!vid_dev)
869                 return -ENODEV;
870
871         if (!acpi_device_dir(device)) {
872                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
873                                                      vid_dev->video->dir);
874                 if (!acpi_device_dir(device))
875                         return -ENODEV;
876                 acpi_device_dir(device)->owner = THIS_MODULE;
877         }
878
879         /* 'info' [R] */
880         entry = create_proc_entry("info", S_IRUGO, acpi_device_dir(device));
881         if (!entry)
882                 return -ENODEV;
883         else {
884                 entry->proc_fops = &acpi_video_device_info_fops;
885                 entry->data = acpi_driver_data(device);
886                 entry->owner = THIS_MODULE;
887         }
888
889         /* 'state' [R/W] */
890         entry =
891             create_proc_entry("state", S_IFREG | S_IRUGO | S_IWUSR,
892                               acpi_device_dir(device));
893         if (!entry)
894                 return -ENODEV;
895         else {
896                 acpi_video_device_state_fops.write = acpi_video_device_write_state;
897                 entry->proc_fops = &acpi_video_device_state_fops;
898                 entry->data = acpi_driver_data(device);
899                 entry->owner = THIS_MODULE;
900         }
901
902         /* 'brightness' [R/W] */
903         entry =
904             create_proc_entry("brightness", S_IFREG | S_IRUGO | S_IWUSR,
905                               acpi_device_dir(device));
906         if (!entry)
907                 return -ENODEV;
908         else {
909                 acpi_video_device_brightness_fops.write = acpi_video_device_write_brightness;
910                 entry->proc_fops = &acpi_video_device_brightness_fops;
911                 entry->data = acpi_driver_data(device);
912                 entry->owner = THIS_MODULE;
913         }
914
915         /* 'EDID' [R] */
916         entry = create_proc_entry("EDID", S_IRUGO, acpi_device_dir(device));
917         if (!entry)
918                 return -ENODEV;
919         else {
920                 entry->proc_fops = &acpi_video_device_EDID_fops;
921                 entry->data = acpi_driver_data(device);
922                 entry->owner = THIS_MODULE;
923         }
924
925         return 0;
926 }
927
928 static int acpi_video_device_remove_fs(struct acpi_device *device)
929 {
930         struct acpi_video_device *vid_dev;
931
932         vid_dev = (struct acpi_video_device *)acpi_driver_data(device);
933         if (!vid_dev || !vid_dev->video || !vid_dev->video->dir)
934                 return -ENODEV;
935
936         if (acpi_device_dir(device)) {
937                 remove_proc_entry("info", acpi_device_dir(device));
938                 remove_proc_entry("state", acpi_device_dir(device));
939                 remove_proc_entry("brightness", acpi_device_dir(device));
940                 remove_proc_entry("EDID", acpi_device_dir(device));
941                 remove_proc_entry(acpi_device_bid(device), vid_dev->video->dir);
942                 acpi_device_dir(device) = NULL;
943         }
944
945         return 0;
946 }
947
948 /* video bus */
949 static int acpi_video_bus_info_seq_show(struct seq_file *seq, void *offset)
950 {
951         struct acpi_video_bus *video = (struct acpi_video_bus *)seq->private;
952
953
954         if (!video)
955                 goto end;
956
957         seq_printf(seq, "Switching heads:              %s\n",
958                    video->flags.multihead ? "yes" : "no");
959         seq_printf(seq, "Video ROM:                    %s\n",
960                    video->flags.rom ? "yes" : "no");
961         seq_printf(seq, "Device to be POSTed on boot:  %s\n",
962                    video->flags.post ? "yes" : "no");
963
964       end:
965         return 0;
966 }
967
968 static int acpi_video_bus_info_open_fs(struct inode *inode, struct file *file)
969 {
970         return single_open(file, acpi_video_bus_info_seq_show,
971                            PDE(inode)->data);
972 }
973
974 static int acpi_video_bus_ROM_seq_show(struct seq_file *seq, void *offset)
975 {
976         struct acpi_video_bus *video = (struct acpi_video_bus *)seq->private;
977
978
979         if (!video)
980                 goto end;
981
982         printk(KERN_INFO PREFIX "Please implement %s\n", __FUNCTION__);
983         seq_printf(seq, "<TODO>\n");
984
985       end:
986         return 0;
987 }
988
989 static int acpi_video_bus_ROM_open_fs(struct inode *inode, struct file *file)
990 {
991         return single_open(file, acpi_video_bus_ROM_seq_show, PDE(inode)->data);
992 }
993
994 static int acpi_video_bus_POST_info_seq_show(struct seq_file *seq, void *offset)
995 {
996         struct acpi_video_bus *video = (struct acpi_video_bus *)seq->private;
997         unsigned long options;
998         int status;
999
1000
1001         if (!video)
1002                 goto end;
1003
1004         status = acpi_video_bus_POST_options(video, &options);
1005         if (ACPI_SUCCESS(status)) {
1006                 if (!(options & 1)) {
1007                         printk(KERN_WARNING PREFIX
1008                                "The motherboard VGA device is not listed as a possible POST device.\n");
1009                         printk(KERN_WARNING PREFIX
1010                                "This indicate a BIOS bug.  Please contact the manufacturer.\n");
1011                 }
1012                 printk("%lx\n", options);
1013                 seq_printf(seq, "can POST: <intgrated video>");
1014                 if (options & 2)
1015                         seq_printf(seq, " <PCI video>");
1016                 if (options & 4)
1017                         seq_printf(seq, " <AGP video>");
1018                 seq_putc(seq, '\n');
1019         } else
1020                 seq_printf(seq, "<not supported>\n");
1021       end:
1022         return 0;
1023 }
1024
1025 static int
1026 acpi_video_bus_POST_info_open_fs(struct inode *inode, struct file *file)
1027 {
1028         return single_open(file, acpi_video_bus_POST_info_seq_show,
1029                            PDE(inode)->data);
1030 }
1031
1032 static int acpi_video_bus_POST_seq_show(struct seq_file *seq, void *offset)
1033 {
1034         struct acpi_video_bus *video = (struct acpi_video_bus *)seq->private;
1035         int status;
1036         unsigned long id;
1037
1038
1039         if (!video)
1040                 goto end;
1041
1042         status = acpi_video_bus_get_POST(video, &id);
1043         if (!ACPI_SUCCESS(status)) {
1044                 seq_printf(seq, "<not supported>\n");
1045                 goto end;
1046         }
1047         seq_printf(seq, "device posted is <%s>\n", device_decode[id & 3]);
1048
1049       end:
1050         return 0;
1051 }
1052
1053 static int acpi_video_bus_DOS_seq_show(struct seq_file *seq, void *offset)
1054 {
1055         struct acpi_video_bus *video = (struct acpi_video_bus *)seq->private;
1056
1057
1058         seq_printf(seq, "DOS setting: <%d>\n", video->dos_setting);
1059
1060         return 0;
1061 }
1062
1063 static int acpi_video_bus_POST_open_fs(struct inode *inode, struct file *file)
1064 {
1065         return single_open(file, acpi_video_bus_POST_seq_show,
1066                            PDE(inode)->data);
1067 }
1068
1069 static int acpi_video_bus_DOS_open_fs(struct inode *inode, struct file *file)
1070 {
1071         return single_open(file, acpi_video_bus_DOS_seq_show, PDE(inode)->data);
1072 }
1073
1074 static ssize_t
1075 acpi_video_bus_write_POST(struct file *file,
1076                           const char __user * buffer,
1077                           size_t count, loff_t * data)
1078 {
1079         int status;
1080         struct seq_file *m = (struct seq_file *)file->private_data;
1081         struct acpi_video_bus *video = (struct acpi_video_bus *)m->private;
1082         char str[12] = { 0 };
1083         unsigned long opt, options;
1084
1085
1086         if (!video || count + 1 > sizeof str)
1087                 return -EINVAL;
1088
1089         status = acpi_video_bus_POST_options(video, &options);
1090         if (!ACPI_SUCCESS(status))
1091                 return -EINVAL;
1092
1093         if (copy_from_user(str, buffer, count))
1094                 return -EFAULT;
1095
1096         str[count] = 0;
1097         opt = strtoul(str, NULL, 0);
1098         if (opt > 3)
1099                 return -EFAULT;
1100
1101         /* just in case an OEM 'forget' the motherboard... */
1102         options |= 1;
1103
1104         if (options & (1ul << opt)) {
1105                 status = acpi_video_bus_set_POST(video, opt);
1106                 if (!ACPI_SUCCESS(status))
1107                         return -EFAULT;
1108
1109         }
1110
1111         return count;
1112 }
1113
1114 static ssize_t
1115 acpi_video_bus_write_DOS(struct file *file,
1116                          const char __user * buffer,
1117                          size_t count, loff_t * data)
1118 {
1119         int status;
1120         struct seq_file *m = (struct seq_file *)file->private_data;
1121         struct acpi_video_bus *video = (struct acpi_video_bus *)m->private;
1122         char str[12] = { 0 };
1123         unsigned long opt;
1124
1125
1126         if (!video || count + 1 > sizeof str)
1127                 return -EINVAL;
1128
1129         if (copy_from_user(str, buffer, count))
1130                 return -EFAULT;
1131
1132         str[count] = 0;
1133         opt = strtoul(str, NULL, 0);
1134         if (opt > 7)
1135                 return -EFAULT;
1136
1137         status = acpi_video_bus_DOS(video, opt & 0x3, (opt & 0x4) >> 2);
1138
1139         if (!ACPI_SUCCESS(status))
1140                 return -EFAULT;
1141
1142         return count;
1143 }
1144
1145 static int acpi_video_bus_add_fs(struct acpi_device *device)
1146 {
1147         struct proc_dir_entry *entry = NULL;
1148         struct acpi_video_bus *video;
1149
1150
1151         video = (struct acpi_video_bus *)acpi_driver_data(device);
1152
1153         if (!acpi_device_dir(device)) {
1154                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1155                                                      acpi_video_dir);
1156                 if (!acpi_device_dir(device))
1157                         return -ENODEV;
1158                 video->dir = acpi_device_dir(device);
1159                 acpi_device_dir(device)->owner = THIS_MODULE;
1160         }
1161
1162         /* 'info' [R] */
1163         entry = create_proc_entry("info", S_IRUGO, acpi_device_dir(device));
1164         if (!entry)
1165                 return -ENODEV;
1166         else {
1167                 entry->proc_fops = &acpi_video_bus_info_fops;
1168                 entry->data = acpi_driver_data(device);
1169                 entry->owner = THIS_MODULE;
1170         }
1171
1172         /* 'ROM' [R] */
1173         entry = create_proc_entry("ROM", S_IRUGO, acpi_device_dir(device));
1174         if (!entry)
1175                 return -ENODEV;
1176         else {
1177                 entry->proc_fops = &acpi_video_bus_ROM_fops;
1178                 entry->data = acpi_driver_data(device);
1179                 entry->owner = THIS_MODULE;
1180         }
1181
1182         /* 'POST_info' [R] */
1183         entry =
1184             create_proc_entry("POST_info", S_IRUGO, acpi_device_dir(device));
1185         if (!entry)
1186                 return -ENODEV;
1187         else {
1188                 entry->proc_fops = &acpi_video_bus_POST_info_fops;
1189                 entry->data = acpi_driver_data(device);
1190                 entry->owner = THIS_MODULE;
1191         }
1192
1193         /* 'POST' [R/W] */
1194         entry =
1195             create_proc_entry("POST", S_IFREG | S_IRUGO | S_IRUSR,
1196                               acpi_device_dir(device));
1197         if (!entry)
1198                 return -ENODEV;
1199         else {
1200                 acpi_video_bus_POST_fops.write = acpi_video_bus_write_POST;
1201                 entry->proc_fops = &acpi_video_bus_POST_fops;
1202                 entry->data = acpi_driver_data(device);
1203                 entry->owner = THIS_MODULE;
1204         }
1205
1206         /* 'DOS' [R/W] */
1207         entry =
1208             create_proc_entry("DOS", S_IFREG | S_IRUGO | S_IRUSR,
1209                               acpi_device_dir(device));
1210         if (!entry)
1211                 return -ENODEV;
1212         else {
1213                 acpi_video_bus_DOS_fops.write = acpi_video_bus_write_DOS;
1214                 entry->proc_fops = &acpi_video_bus_DOS_fops;
1215                 entry->data = acpi_driver_data(device);
1216                 entry->owner = THIS_MODULE;
1217         }
1218
1219         return 0;
1220 }
1221
1222 static int acpi_video_bus_remove_fs(struct acpi_device *device)
1223 {
1224         struct acpi_video_bus *video;
1225
1226
1227         video = (struct acpi_video_bus *)acpi_driver_data(device);
1228
1229         if (acpi_device_dir(device)) {
1230                 remove_proc_entry("info", acpi_device_dir(device));
1231                 remove_proc_entry("ROM", acpi_device_dir(device));
1232                 remove_proc_entry("POST_info", acpi_device_dir(device));
1233                 remove_proc_entry("POST", acpi_device_dir(device));
1234                 remove_proc_entry("DOS", acpi_device_dir(device));
1235                 remove_proc_entry(acpi_device_bid(device), acpi_video_dir);
1236                 acpi_device_dir(device) = NULL;
1237         }
1238
1239         return 0;
1240 }
1241
1242 /* --------------------------------------------------------------------------
1243                                  Driver Interface
1244    -------------------------------------------------------------------------- */
1245
1246 /* device interface */
1247
1248 static int
1249 acpi_video_bus_get_one_device(struct acpi_device *device,
1250                               struct acpi_video_bus *video)
1251 {
1252         unsigned long device_id;
1253         int status;
1254         struct acpi_video_device *data;
1255
1256
1257         if (!device || !video)
1258                 return -EINVAL;
1259
1260         status =
1261             acpi_evaluate_integer(device->handle, "_ADR", NULL, &device_id);
1262         if (ACPI_SUCCESS(status)) {
1263
1264                 data = kmalloc(sizeof(struct acpi_video_device), GFP_KERNEL);
1265                 if (!data)
1266                         return -ENOMEM;
1267
1268                 memset(data, 0, sizeof(struct acpi_video_device));
1269
1270                 strcpy(acpi_device_name(device), ACPI_VIDEO_DEVICE_NAME);
1271                 strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1272                 acpi_driver_data(device) = data;
1273
1274                 data->device_id = device_id;
1275                 data->video = video;
1276                 data->dev = device;
1277
1278                 switch (device_id & 0xffff) {
1279                 case 0x0100:
1280                         data->flags.crt = 1;
1281                         break;
1282                 case 0x0400:
1283                         data->flags.lcd = 1;
1284                         break;
1285                 case 0x0200:
1286                         data->flags.tvout = 1;
1287                         break;
1288                 default:
1289                         data->flags.unknown = 1;
1290                         break;
1291                 }
1292
1293                 acpi_video_device_bind(video, data);
1294                 acpi_video_device_find_cap(data);
1295
1296                 status = acpi_install_notify_handler(device->handle,
1297                                                      ACPI_DEVICE_NOTIFY,
1298                                                      acpi_video_device_notify,
1299                                                      data);
1300                 if (ACPI_FAILURE(status)) {
1301                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1302                                           "Error installing notify handler\n"));
1303                         if(data->brightness)
1304                                 kfree(data->brightness->levels);
1305                         kfree(data->brightness);
1306                         kfree(data);
1307                         return -ENODEV;
1308                 }
1309
1310                 down(&video->sem);
1311                 list_add_tail(&data->entry, &video->video_device_list);
1312                 up(&video->sem);
1313
1314                 acpi_video_device_add_fs(device);
1315
1316                 return 0;
1317         }
1318
1319         return -ENOENT;
1320 }
1321
1322 /*
1323  *  Arg:
1324  *      video   : video bus device 
1325  *
1326  *  Return:
1327  *      none
1328  *  
1329  *  Enumerate the video device list of the video bus, 
1330  *  bind the ids with the corresponding video devices
1331  *  under the video bus.
1332  */
1333
1334 static void acpi_video_device_rebind(struct acpi_video_bus *video)
1335 {
1336         struct list_head *node, *next;
1337         list_for_each_safe(node, next, &video->video_device_list) {
1338                 struct acpi_video_device *dev =
1339                     container_of(node, struct acpi_video_device, entry);
1340                 acpi_video_device_bind(video, dev);
1341         }
1342 }
1343
1344 /*
1345  *  Arg:
1346  *      video   : video bus device 
1347  *      device  : video output device under the video 
1348  *              bus
1349  *
1350  *  Return:
1351  *      none
1352  *  
1353  *  Bind the ids with the corresponding video devices
1354  *  under the video bus.
1355  */
1356
1357 static void
1358 acpi_video_device_bind(struct acpi_video_bus *video,
1359                        struct acpi_video_device *device)
1360 {
1361         int i;
1362
1363 #define IDS_VAL(i) video->attached_array[i].value.int_val
1364 #define IDS_BIND(i) video->attached_array[i].bind_info
1365
1366         for (i = 0; IDS_VAL(i) != ACPI_VIDEO_HEAD_INVALID &&
1367              i < video->attached_count; i++) {
1368                 if (device->device_id == (IDS_VAL(i) & 0xffff)) {
1369                         IDS_BIND(i) = device;
1370                         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "device_bind %d\n", i));
1371                 }
1372         }
1373 #undef IDS_VAL
1374 #undef IDS_BIND
1375 }
1376
1377 /*
1378  *  Arg:
1379  *      video   : video bus device 
1380  *
1381  *  Return:
1382  *      < 0     : error
1383  *  
1384  *  Call _DOD to enumerate all devices attached to display adapter
1385  *
1386  */
1387
1388 static int acpi_video_device_enumerate(struct acpi_video_bus *video)
1389 {
1390         int status;
1391         int count;
1392         int i;
1393         struct acpi_video_enumerated_device *active_device_list;
1394         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1395         union acpi_object *dod = NULL;
1396         union acpi_object *obj;
1397
1398         status = acpi_evaluate_object(video->device->handle, "_DOD", NULL, &buffer);
1399         if (!ACPI_SUCCESS(status)) {
1400                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _DOD"));
1401                 return status;
1402         }
1403
1404         dod = (union acpi_object *)buffer.pointer;
1405         if (!dod || (dod->type != ACPI_TYPE_PACKAGE)) {
1406                 ACPI_EXCEPTION((AE_INFO, status, "Invalid _DOD data"));
1407                 status = -EFAULT;
1408                 goto out;
1409         }
1410
1411         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d video heads in _DOD\n",
1412                           dod->package.count));
1413
1414         active_device_list = kmalloc((1 +
1415                                       dod->package.count) *
1416                                      sizeof(struct
1417                                             acpi_video_enumerated_device),
1418                                      GFP_KERNEL);
1419
1420         if (!active_device_list) {
1421                 status = -ENOMEM;
1422                 goto out;
1423         }
1424
1425         count = 0;
1426         for (i = 0; i < dod->package.count; i++) {
1427                 obj = (union acpi_object *)&dod->package.elements[i];
1428
1429                 if (obj->type != ACPI_TYPE_INTEGER) {
1430                         printk(KERN_ERR PREFIX "Invalid _DOD data\n");
1431                         active_device_list[i].value.int_val =
1432                             ACPI_VIDEO_HEAD_INVALID;
1433                 }
1434                 active_device_list[i].value.int_val = obj->integer.value;
1435                 active_device_list[i].bind_info = NULL;
1436                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "dod element[%d] = %d\n", i,
1437                                   (int)obj->integer.value));
1438                 count++;
1439         }
1440         active_device_list[count].value.int_val = ACPI_VIDEO_HEAD_END;
1441
1442         kfree(video->attached_array);
1443
1444         video->attached_array = active_device_list;
1445         video->attached_count = count;
1446       out:
1447         kfree(buffer.pointer);
1448         return status;
1449 }
1450
1451 /*
1452  *  Arg:
1453  *      video   : video bus device 
1454  *      event   : Nontify Event
1455  *
1456  *  Return:
1457  *      < 0     : error
1458  *  
1459  *      1. Find out the current active output device.
1460  *      2. Identify the next output device to switch
1461  *      3. call _DSS to do actual switch.
1462  */
1463
1464 static int acpi_video_switch_output(struct acpi_video_bus *video, int event)
1465 {
1466         struct list_head *node, *next;
1467         struct acpi_video_device *dev = NULL;
1468         struct acpi_video_device *dev_next = NULL;
1469         struct acpi_video_device *dev_prev = NULL;
1470         unsigned long state;
1471         int status = 0;
1472
1473
1474         list_for_each_safe(node, next, &video->video_device_list) {
1475                 dev = container_of(node, struct acpi_video_device, entry);
1476                 status = acpi_video_device_get_state(dev, &state);
1477                 if (state & 0x2) {
1478                         dev_next =
1479                             container_of(node->next, struct acpi_video_device,
1480                                          entry);
1481                         dev_prev =
1482                             container_of(node->prev, struct acpi_video_device,
1483                                          entry);
1484                         goto out;
1485                 }
1486         }
1487         dev_next = container_of(node->next, struct acpi_video_device, entry);
1488         dev_prev = container_of(node->prev, struct acpi_video_device, entry);
1489       out:
1490         switch (event) {
1491         case ACPI_VIDEO_NOTIFY_CYCLE:
1492         case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:
1493                 acpi_video_device_set_state(dev, 0);
1494                 acpi_video_device_set_state(dev_next, 0x80000001);
1495                 break;
1496         case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:
1497                 acpi_video_device_set_state(dev, 0);
1498                 acpi_video_device_set_state(dev_prev, 0x80000001);
1499         default:
1500                 break;
1501         }
1502
1503         return status;
1504 }
1505
1506 static int
1507 acpi_video_get_next_level(struct acpi_video_device *device,
1508                           u32 level_current, u32 event)
1509 {
1510         /*Fix me */
1511         return level_current;
1512 }
1513
1514 static void
1515 acpi_video_switch_brightness(struct acpi_video_device *device, int event)
1516 {
1517         unsigned long level_current, level_next;
1518         acpi_video_device_lcd_get_level_current(device, &level_current);
1519         level_next = acpi_video_get_next_level(device, level_current, event);
1520         acpi_video_device_lcd_set_level(device, level_next);
1521 }
1522
1523 static int
1524 acpi_video_bus_get_devices(struct acpi_video_bus *video,
1525                            struct acpi_device *device)
1526 {
1527         int status = 0;
1528         struct list_head *node, *next;
1529
1530
1531         acpi_video_device_enumerate(video);
1532
1533         list_for_each_safe(node, next, &device->children) {
1534                 struct acpi_device *dev =
1535                     list_entry(node, struct acpi_device, node);
1536
1537                 if (!dev)
1538                         continue;
1539
1540                 status = acpi_video_bus_get_one_device(dev, video);
1541                 if (ACPI_FAILURE(status)) {
1542                         ACPI_EXCEPTION((AE_INFO, status, "Cant attach device"));
1543                         continue;
1544                 }
1545
1546         }
1547         return status;
1548 }
1549
1550 static int acpi_video_bus_put_one_device(struct acpi_video_device *device)
1551 {
1552         acpi_status status;
1553         struct acpi_video_bus *video;
1554
1555
1556         if (!device || !device->video)
1557                 return -ENOENT;
1558
1559         video = device->video;
1560
1561         down(&video->sem);
1562         list_del(&device->entry);
1563         up(&video->sem);
1564         acpi_video_device_remove_fs(device->dev);
1565
1566         status = acpi_remove_notify_handler(device->dev->handle,
1567                                             ACPI_DEVICE_NOTIFY,
1568                                             acpi_video_device_notify);
1569
1570         return 0;
1571 }
1572
1573 static int acpi_video_bus_put_devices(struct acpi_video_bus *video)
1574 {
1575         int status;
1576         struct list_head *node, *next;
1577
1578
1579         list_for_each_safe(node, next, &video->video_device_list) {
1580                 struct acpi_video_device *data =
1581                     list_entry(node, struct acpi_video_device, entry);
1582                 if (!data)
1583                         continue;
1584
1585                 status = acpi_video_bus_put_one_device(data);
1586                 if (ACPI_FAILURE(status))
1587                         printk(KERN_WARNING PREFIX
1588                                "hhuuhhuu bug in acpi video driver.\n");
1589
1590                 if (data->brightness)
1591                         kfree(data->brightness->levels);
1592                 kfree(data->brightness);
1593                 kfree(data);
1594         }
1595
1596         return 0;
1597 }
1598
1599 /* acpi_video interface */
1600
1601 static int acpi_video_bus_start_devices(struct acpi_video_bus *video)
1602 {
1603         return acpi_video_bus_DOS(video, 1, 0);
1604 }
1605
1606 static int acpi_video_bus_stop_devices(struct acpi_video_bus *video)
1607 {
1608         return acpi_video_bus_DOS(video, 0, 1);
1609 }
1610
1611 static void acpi_video_bus_notify(acpi_handle handle, u32 event, void *data)
1612 {
1613         struct acpi_video_bus *video = (struct acpi_video_bus *)data;
1614         struct acpi_device *device = NULL;
1615
1616         printk("video bus notify\n");
1617
1618         if (!video)
1619                 return;
1620
1621         device = video->device;
1622
1623         switch (event) {
1624         case ACPI_VIDEO_NOTIFY_SWITCH:  /* User request that a switch occur,
1625                                          * most likely via hotkey. */
1626                 acpi_bus_generate_event(device, event, 0);
1627                 break;
1628
1629         case ACPI_VIDEO_NOTIFY_PROBE:   /* User plug or remove a video
1630                                          * connector. */
1631                 acpi_video_device_enumerate(video);
1632                 acpi_video_device_rebind(video);
1633                 acpi_video_switch_output(video, event);
1634                 acpi_bus_generate_event(device, event, 0);
1635                 break;
1636
1637         case ACPI_VIDEO_NOTIFY_CYCLE:   /* Cycle Display output hotkey pressed. */
1638         case ACPI_VIDEO_NOTIFY_NEXT_OUTPUT:     /* Next Display output hotkey pressed. */
1639         case ACPI_VIDEO_NOTIFY_PREV_OUTPUT:     /* previous Display output hotkey pressed. */
1640                 acpi_video_switch_output(video, event);
1641                 acpi_bus_generate_event(device, event, 0);
1642                 break;
1643
1644         default:
1645                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1646                                   "Unsupported event [0x%x]\n", event));
1647                 break;
1648         }
1649
1650         return;
1651 }
1652
1653 static void acpi_video_device_notify(acpi_handle handle, u32 event, void *data)
1654 {
1655         struct acpi_video_device *video_device =
1656             (struct acpi_video_device *)data;
1657         struct acpi_device *device = NULL;
1658
1659
1660         printk("video device notify\n");
1661         if (!video_device)
1662                 return;
1663
1664         device = video_device->dev;
1665
1666         switch (event) {
1667         case ACPI_VIDEO_NOTIFY_SWITCH:  /* change in status (cycle output device) */
1668         case ACPI_VIDEO_NOTIFY_PROBE:   /* change in status (output device status) */
1669                 acpi_bus_generate_event(device, event, 0);
1670                 break;
1671         case ACPI_VIDEO_NOTIFY_CYCLE_BRIGHTNESS:        /* Cycle brightness */
1672         case ACPI_VIDEO_NOTIFY_INC_BRIGHTNESS:  /* Increase brightness */
1673         case ACPI_VIDEO_NOTIFY_DEC_BRIGHTNESS:  /* Decrease brightness */
1674         case ACPI_VIDEO_NOTIFY_ZERO_BRIGHTNESS: /* zero brightnesss */
1675         case ACPI_VIDEO_NOTIFY_DISPLAY_OFF:     /* display device off */
1676                 acpi_video_switch_brightness(video_device, event);
1677                 acpi_bus_generate_event(device, event, 0);
1678                 break;
1679         default:
1680                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1681                                   "Unsupported event [0x%x]\n", event));
1682                 break;
1683         }
1684         return;
1685 }
1686
1687 static int acpi_video_bus_add(struct acpi_device *device)
1688 {
1689         int result = 0;
1690         acpi_status status = 0;
1691         struct acpi_video_bus *video = NULL;
1692
1693
1694         if (!device)
1695                 return -EINVAL;
1696
1697         video = kmalloc(sizeof(struct acpi_video_bus), GFP_KERNEL);
1698         if (!video)
1699                 return -ENOMEM;
1700         memset(video, 0, sizeof(struct acpi_video_bus));
1701
1702         video->device = device;
1703         strcpy(acpi_device_name(device), ACPI_VIDEO_BUS_NAME);
1704         strcpy(acpi_device_class(device), ACPI_VIDEO_CLASS);
1705         acpi_driver_data(device) = video;
1706
1707         acpi_video_bus_find_cap(video);
1708         result = acpi_video_bus_check(video);
1709         if (result)
1710                 goto end;
1711
1712         result = acpi_video_bus_add_fs(device);
1713         if (result)
1714                 goto end;
1715
1716         init_MUTEX(&video->sem);
1717         INIT_LIST_HEAD(&video->video_device_list);
1718
1719         acpi_video_bus_get_devices(video, device);
1720         acpi_video_bus_start_devices(video);
1721
1722         status = acpi_install_notify_handler(device->handle,
1723                                              ACPI_DEVICE_NOTIFY,
1724                                              acpi_video_bus_notify, video);
1725         if (ACPI_FAILURE(status)) {
1726                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1727                                   "Error installing notify handler\n"));
1728                 acpi_video_bus_stop_devices(video);
1729                 acpi_video_bus_put_devices(video);
1730                 kfree(video->attached_array);
1731                 acpi_video_bus_remove_fs(device);
1732                 result = -ENODEV;
1733                 goto end;
1734         }
1735
1736         printk(KERN_INFO PREFIX "%s [%s] (multi-head: %s  rom: %s  post: %s)\n",
1737                ACPI_VIDEO_DEVICE_NAME, acpi_device_bid(device),
1738                video->flags.multihead ? "yes" : "no",
1739                video->flags.rom ? "yes" : "no",
1740                video->flags.post ? "yes" : "no");
1741
1742       end:
1743         if (result)
1744                 kfree(video);
1745
1746         return result;
1747 }
1748
1749 static int acpi_video_bus_remove(struct acpi_device *device, int type)
1750 {
1751         acpi_status status = 0;
1752         struct acpi_video_bus *video = NULL;
1753
1754
1755         if (!device || !acpi_driver_data(device))
1756                 return -EINVAL;
1757
1758         video = (struct acpi_video_bus *)acpi_driver_data(device);
1759
1760         acpi_video_bus_stop_devices(video);
1761
1762         status = acpi_remove_notify_handler(video->device->handle,
1763                                             ACPI_DEVICE_NOTIFY,
1764                                             acpi_video_bus_notify);
1765
1766         acpi_video_bus_put_devices(video);
1767         acpi_video_bus_remove_fs(device);
1768
1769         kfree(video->attached_array);
1770         kfree(video);
1771
1772         return 0;
1773 }
1774
1775 static int __init acpi_video_init(void)
1776 {
1777         int result = 0;
1778
1779
1780         /*
1781            acpi_dbg_level = 0xFFFFFFFF;
1782            acpi_dbg_layer = 0x08000000;
1783          */
1784
1785         acpi_video_dir = proc_mkdir(ACPI_VIDEO_CLASS, acpi_root_dir);
1786         if (!acpi_video_dir)
1787                 return -ENODEV;
1788         acpi_video_dir->owner = THIS_MODULE;
1789
1790         result = acpi_bus_register_driver(&acpi_video_bus);
1791         if (result < 0) {
1792                 remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
1793                 return -ENODEV;
1794         }
1795
1796         return 0;
1797 }
1798
1799 static void __exit acpi_video_exit(void)
1800 {
1801
1802         acpi_bus_unregister_driver(&acpi_video_bus);
1803
1804         remove_proc_entry(ACPI_VIDEO_CLASS, acpi_root_dir);
1805
1806         return;
1807 }
1808
1809 module_init(acpi_video_init);
1810 module_exit(acpi_video_exit);